开发者

Do Java or C++ lack any OO features?

I am interested in understanding object-oriented programming in a more academic and abstract way than I currently do, and want to know if t开发者_高级运维here are any object-oriented concepts Java and C++ fail to implement.

I realise neither of the languages are "pure" OO, but I am interested in what (if anything) they lack, not what they have extra.


In the words of Alan Kay, the inventor of the term "object orientation":

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them.

C++ obviously fails the "extreme late-binding" criterium, and both Java and C++ fail the "messaging" criterium, due to their rigid class/method structure. As I understand it, Kay's concept considers methods with a specific name and signature a convenient way to implement message handlers, but by no means the only one.

Other interesting statements from the same email:

I didn't like the way Simula I or Simula 67 did inheritance [...] So I decided to leave out inheritance as a built-in feature until I understood it better.

and

The term "polymorphism" was imposed much later (I think by Peter Wegner) and it isn't quite valid, since it really comes from the nomenclature of functions, and I wanted quite a bit more than functions.


Off the top of my head, I'd say:

  • multiple dispatch
  • generic functions
  • a metaobject protocol
  • being able to subclass native types (I don't know that this has a name, because there are relatively few OO languages I know which don't allow this)


Both make a distinction between primitives and objects, so neither are purely object-oriented.


There's another way of thinking about object oriented programming that differs from the class-based system in Java and C++. Prototype based programming is used by JavaScript. If you want to look at the full gamut of OOP styles, it's probably worth taking a look at: http://en.wikipedia.org/wiki/Prototype-based_programming


Java : Primitive types are not objects.


Java doesn't have multiple inheritance, but some might say this is more of a blessing as it requires users to think of correct architecture. You can use interfaces and abstract classes to get around this.

Multiple inheritance has been criticised for the following problems that it causes in certain languages, in particular C++:

  • Semantic ambiguity often summarized as the diamond problem.
  • Not being able to explicitly inherit multiple times from a single class
  • Order of inheritance changing class semantics


The single most important feature of object oriented programming is encapsulation. Hiding implementation details is obviously crucial to writing maintainable code.

In C++, since you have uncontrolled pointers, it is possible for one badly written object to do literally anything to another. This means that encapsulation is broken, and that bugs are difficult to find.

Java doesn't have that problem, but it lacks basic const-ness. That's not strictly an object-oriented theoretical feature, but being able to declare that a method is read only, or that an object is read only, is a fantastic reliability enhancer in C++ that is not in Java.

Last, java's template mechanism is a pale imitation of C++. Not being able to parametrize classes is a huge loss for Java.

Because Java doesn't support pointers to methods, and reflection is too slow, it forces the use of many little objects when a function pointer would do. Some may consider that a good thing.


Java and C++ both allow procedural programming. That can be considered to be a minus for some people.


Most interpreted languages meet the late-binding requirements. In Perl you can pull code out of a database table, throw it into the interpreter, then instantiate objects of the new class you've just introduced into the program.

Does Python fully meet Kay's definition of OOP? I've not done enough work in Python to be sure. I suspect not, since Python has 'native types' that are not objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜