tags:

views:

358

answers:

9

I am interested in understanding object-oriented programming in a more academic and abstract way than I currently do, and want to know if there 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.

+1  A: 

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
Chris Dennett
@Chris Dennett: this is simply not true. Implementation is a detail and you need to know that you can *trivially* translate *any* OOA/OOD using *any* crazy multiple inheritance very cleanly in Java (as you can do in language that don't even have the concept of implementation inheritance) using multiple interface inheritance. You're probably confused with "code reuse" which you can trivially achieve using delegation. 200KLOC Java codebase here where we're using exactly *zero* time implementation inheritance... And where we use multiple inheritance *everywhere*.
Webinator
Now you're nitpicking. I already mentioned this.
Chris Dennett
OK, WizardOfOdds, Java is missing the interface delegation. If you want some class to delegate interface I to object O, you have to manually create all the methods of I that just trivally call the same methods of O.
Gabe
A: 

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

fastcodejava
I'd in fact argue that most Java/C++ code *is* procedural, with an OO veneer on it.
kyoryu
+1  A: 

Java : Primitive types are not objects.

Tuomas Pelkonen
That's not a feature that's lacking, it's a language design idiosyncrasy.
Benson
Well, some people (me) call bugs missing features :)
Tuomas Pelkonen
You can't write `class MyInt: public int` in C++ either. Not if you want it to compile, anyway.
David Thornley
You can think of it as a multirooted hierarchy if you like. :)
Tom Hawtin - tackline
At least since java 1.5 there is autoboxing allowing easy interchange of for example int primatives with Integer objects. I agree though that it prevents java from being, in the strictest sense, a fully Object Oriented language
Alb
@Tuomas Pelkonen: Don't you mean that bugs are *undocumented* features?
Eddie
@Eddie Sometimes undocumented or sometimes unimplemented features.
Tuomas Pelkonen
+15  A: 

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.

Michael Borgwardt
Doesn't Java also fail the former criterion?
Ken
What other languages today would you say satisfy the "extreme late-binding" criteria? Still only Smalltalk and LISP?
FrustratedWithFormsDesigner
FWFD: I think Ruby would qualify on that count.
Ken
@FrustratedWithFormsDesigner: JavaScript, Ruby for example.
Alexander Pogrebnyak
I'm not completely sure what he means with that term. Java's binding is IMO very late (classes are loaded when they're first referenced), though I guess one could see that as still too early since it fixes the class signature before it's actually used (ties in with the "messaging" aspect). Truly dynamic languages like Python, Groovy or Ruby probably qualify.
Michael Borgwardt
@FWFD: With that criterium, Objective-C should count as well.
Georg Fritzsche
@gf: Objective-C passes that test?
FrustratedWithFormsDesigner
One important word here is "all" things. In Smalltalk-71, for example, syntax was late-bound. In later versions of the language, Smalltalk actually became a lot more static, which is why Alan Kay wanted to start over with a completely new approach. In fact, Smalltalk-76 and onwards were no longer designed by him, and they do not fully embody his vision of OOP, and neither do their successors, like Ruby. The language he is currently working on, is much more late-bound and dynamic than either Smalltalk or Lisp, where in fact even the definition of "late-bound" itself is late-bound.
Jörg W Mittag
@Jörg W Mittag: sounds kinda cool. What is the name of that language?
FrustratedWithFormsDesigner
@FWFD: With ObjC you can dynamically choose what messages to respond to, pass them up the inheritance chain or pass them to completely different objects - so unless i'm missing something, yes.
Georg Fritzsche
@FrustratedWithFormsDesigner: I don't think it has a name yet. You can find some information at the Research Institute that Alan Kay founded: http://VPRI.Org/ Also, just like Smalltalk it's not just a language, it's an entire Personal Computing System, consisting of an Operating System, Drivers, Graphical User Interface, Language, Compiler, Office Suite, Desktop Publishing, Networking, ... And the amazing thing: all of that in less than 20 thousand lines of code. Look at the papers on the VPRI website, or one of the videos on the web.
Jörg W Mittag
How Complex is "Personal Computing": http://softwareengineering.vazexqi.com/2009/10/22/how-complex-is-personal-computing---by-the-fine-folks-from-vpriTED Talk "Alan Kay shares a powerful idea about ideas": http://www.ted.com/talks/alan_kay_shares_a_powerful_idea_about_ideas.htmlHow Simply and Understandably Could The "Personal Computing Experience" Be Programmed: http://irbseminars.intel-research.net/AlanKay.wmv
Jörg W Mittag
People don't get just *how* OO Smalltalk was. In Smalltalk, the equivalent of `new Foo()` is `Foo new`. But it's not, really - in fact, in Smalltalk, that is a message sent to the singleton object Foo, which is a Class object. In Smalltalk, `if` is a message sent to an object. Java and C# *say* everything is an object... Smalltalk *means* it.
kyoryu
+2  A: 

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

Benson
+3  A: 

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

JRL
@JRL: that's in no way "lacking a feature" and the question especially states that the poster *knows* that C++ and Java aren't pure OO languages.
Webinator
+7  A: 

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

Ken
A: 

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.

Wexxor
Which "native types" are you referring to? I've not run into anything in Python that's not an object.
Benson
I stand corrected. From the Tutorial at DiveIntoPython.org:"2.4.2. What's an Object?Everything in Python is an object, and almost everything has attributes and methods."I was probably thinking of regular variables, which don't have any attributes by default. They are still objects. Yay Python.
Wexxor
+1  A: 

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.

Dov