tags:

views:

491

answers:

14

I noticed after reading the answers/discussion to this question (What is the best language to learn OOP on?) - that more and more people are recommending C# or Java over C++ to learn OOP on. A simple term search on that answer page results in 10 hits for C++, 21 for C# and 27 for Java.

Now, I understand that these 2 languages fix a lot of quirks and issues with C++, and looked up these resources that relate mostly to performance, JVM vs native implementation, systems focus vs applications, manual memory management vs automated et al.

My question is - are there any fundamental differences in the OO capabilities of Java/C# vs C++? Or are the former recommended purely due to their generic ease of use/improvements over the latter?

Thanks.

PS, I'm aware of Java interface inheritance vs C++ multiple inheritance as a difference. I would consider that an implementational one rather than functional.


Wow, I wasn't expecting these many answers! Thanks a lot. I'll update with a summary here of what I learned.

+1  A: 

C# and Java are preferred for ease of use.

Are there fundamental differences? No. you get all the OO basics.

There are some differences, as you know, but not enough to consider Java or C# as "inferior" OO languages.

Cheeso
+1  A: 

People recommend these languages for learning because C++ is more "do-it-yourself-y", a trait that tends to make beginners uncomfortable. Java and C# manage a lot of things for the programmer while still providing reasonable language facilities for their specific problem domains. When a person is comfortable with programming concepts, language is practically immaterial except perhaps to more clearly solve a specific problem. Hence it makes a lot more sense for beginners to learn languages that they can easily understand and from which they can build their knowledge.

Jon Purdy
+2  A: 

Because they are safe languages and you don't have to worry about overflowing buffers, leaking memory, causing undefined behavior, etc. That way you can focus on learning OOP and not have to worry about shooting yourself in the foot.

R Samuel Klatchko
+4  A: 

C++ doesn't force you to program in an OOP style while Java much more does so. If you want to learn to program in an object oriented way it helps if the language is pushing you in that direction.

sth
This is very true; in C++, "you don't get what you don't pay for" holds even for OOP.
Jon Purdy
If you really want pure OO then you may as well go with SmallTalk. I would say it is because C# and Java are managed memory languages. You can still write crappy, procedural code in either one.
Keith Rousseau
@Keith You can write FORTRAN in any language, including Smalltalk.
Tom Hawtin - tackline
+3  A: 

One of the major advantages of C# and Java over C++ when using it for learning OOP is that the learner does not have to worry about garbage collection issues yet, as garbage collection is automatic and implicit in both C# and Java.

Jon Limjap
Technically a beginner doesn't have to worry about garbage collection in C++ either. As long as your focus is simply on learning OOP, rather than on writing good C++ code, you can just leak memory and ignore the issue.
jalf
I wish I could downvote a comment...
Steve
It's almost more like, if you learn OOP and use that to make sure your pointers does not leak, you on your way.
jpyllman
@Steve: why? I'm just taking the question at face value. Of course, if we're interested solely in learning OOP, with no regard for *anything* else, then we can ignore memory leaks. I'm obviously not suggesting that it's a sane strategy for writing *good* C++ code. Just that it's possible to learn OOP even though your code leaks memory. (and I'd wager that virtually anyone who did learn OOP in C++ have done it by writing code that leaked memory. They just didn't realize it because the leak was too subtle for them to find and fix)
jalf
+4  A: 

One of the big things that you get with Java or C# over C++ is a large standard framework.

Want to do networking - it's in the framework, In C++, you'll need to roll your own or learn, install and integrate some other framework that isn't necessarily what others might be using.

Same for any number of other sets of functionality.

Of course C++ has Boost, but that's generally much, much lower-level than you you get baked into the Java or .Net frameworks.

Michael Burr
Does the larger framework help you *in learning OOP* though? That was the OP's specific question after all.
jalf
@jalf - it helps you to DO more things, which makes the motivation to continue learning greater
Steve
It certainly helps implementing patterns like observer in C#. ObserverableCollection<T> is quite handy (http://msdn.microsoft.com/en-us/library/ms668604.aspx)
Claus Jørgensen
@jalf - I think it's mainly a motivator like Steve says, but one area where it helps with learning OOP is that presumably the frameworks themselves could be considered reference OOP designs. While I'm sure there are plenty of things that might not be great examples of object oriented purity, they're definitely a good area to learn a lot from (I sometimes even steal ideas from them to use in C++).
Michael Burr
@Michael: Ok, that's a good point. Hadn't considered that. And while I think there are plenty of things to learn from the C++ standard library (or Boost, as you said in your answer), it's generally not the OOP bits that really shine.
jalf
+11  A: 

Java is higher-level language compared to C++. It abstract you much more from the machine and this is great for learning purpose. You do NOT want students to deal with pointer problems, overflow, garbage collections, etc while learning OOP concepts, since they are orthogonal issue to OOP and will distract the student's attention away from the core concept of OOP itself.

I don't have experience using C#, so I can't talk much about it.

Lie Ryan
Thank you for actually answering the question instead of descending into FUD. +1
Billy ONeal
+4  A: 

I can only speak for myself, but for learning OOP, I'd recommend C# or Java over C++ because C++ isn't an OOP language.

C++ allows you to mix a lot of styles, including a few OOP bits and pieces. But any sane C++ code I've seen is not predominantly OOP.

C# and Java are all about OOP. If your code doesn't follow the rules of OOP, you're doing it wrong.

Another reason is almost as obvious:
C++ is a pain to learn. It's very subtle and tricky to get right. And if you mess up, you most likely won't get a nice compiler error or exception thrown at runtime. Most likely, your program will just jump off the rails and do weird unpredictable things. Maybe crash, maybe seem to work, maybe just seem to do the wrong thing. Or often, seem to work, and then crash somewhere entirely different 5 minutes later.

C# and Java pretty much eliminate undefined behavior. If you do something that's not allowed by the language, the error is caught one way or the other. That simplifies things a lot, especially for a beginner.

jalf
re, pain to learn: it's definately true that common, current generation compilers like gcc has a pretty unforgiving/unhelpful compiler. the new CLang compiler for LLVM, on the other hand...much faster, and much nicer messages, making it much easier to use.
Robert P
@Robert: I haven't tried it yet, but I've heard that it's quite promising. How advanced is it in terms of standard compliance? I was under the impression it was still missing quite a few features.
jalf
+2  A: 

You're ignoring the elephant in the room.

The #1 answer (and #4, and #7, and #9, and ...) on the question you linked to says Java/C# for work, but Smalltalk/Self for really learning OOP, and all of its comments seemed to agree with the Smalltalk recommendation.

From this perspective, the answer is simple: C# and Java are a lot closer to Smalltalk than C++ is.

Ken
Are they? I can't see that they have much more in common with Smalltalk than C++ does.
jalf
+3  A: 

are there any fundamental differences in the OO capabilities of Java/C# vs C++?

Yes. Generics, Properties, Events, and so on. You mentioned MI yourself.

C# is probably the better language to learn it in, as properties gives a much more clear understanding of what ensapsulation is all about, and is far less confusing than methods for the same.

The question is possible how you define OOP. One could argue that events are related (think observer pattern), and thus the easy handling of events in Java and C# (specially C#) is a difference.

And then C++ just sucks when it comes to OOP. I mean, header files? Please, this is 2010, they should be compiler generated already!

Edit: Also that Java and C# uses Exceptions for everything is a big plus. C++'s exception model isn't that great.

Claus Jørgensen
+1 for compiler generated header file comment. That alone just ruins C++/Obj-C in my opinion
Joel
Ahem. C++ generic programming is way way beyond what either Java or C# can offer. Properties as syntactic sugar have very little to do with O-O attributes. RAII dominates both Java and C# exception handling. Header files encourage good design, you have to think about the distinction between implementation and interface (and many Java interfaces are pretty much identical to what would be in the header file for a C++ class). C++ virtual functions are just as good for event handling as anything Java can offer, and template algorithms accepting functors are much better. Did I miss anything?
Ben Voigt
-1 Properties confuse the understanding of encapsulation.
Tom Hawtin - tackline
That's of course, opinions, and a discussion for another day. But try see it in the context of the question. And what is C++ today anyways? It's pretty vague for a language with so many extensions (like C++.NET)
Claus Jørgensen
Downvoted for anti C++ FUD. This answer reads like a laundry list of things Claus Jørgensen dislikes about the language, rather than an answer to the OP's specific question.
Billy ONeal
"so many extensions"? There is only one C++ 98 standard and the upcoming C++0x will establish the next one.C++/CLI is the only notable C++ extension that is in use, so this is just FUD as well.
Axel Gneiting
Downvoted for being somewhat confused about what OOP *is* (properties don't really give a clearer understanding of encapsulation), and I fail to see how header files have *anything* to do with OOP. They're a drawback of the language as a whole, but they have *nothing* to do with OOP. (Same for exceptions. Since when did exceptions have anything to do with OOP?)
jalf
As for C++ being "vague", hardly. How many versions of C# do you have? 4 with the release of VS10? C++ has had *two*. C++98 and C++03. And they're virtually identical, with only a few bugfixes separating them. C++/CLI has nothing to do with C++. It is a different language that just so happens to be largely source compatible. But then C is largely source compatible with C++ too. That doesn't mean they're the same language. Hell, Java and C# are largely source compatible.
jalf
+3  A: 

Java/C# are almost pure OO languages. This means that they are largely designed to implement programs using OO methodology exclusively.

C++ is a multi-paradigm language, and has no preferred methodology. It is possible to implement OO using C++, but there are considerable deployment issues with this. This is because there is no C++ VM. The VM is pretty much what makes OO practical, since it can cope with assemblies, run time introspection, etc. that are required to deploy OO modules. There have been attempts to wrap C++ up like this (COM,CORBA,SOM) but these didnt work so well.

However, for academic purposes I think the virtual inheritance mechanism is how OO should be taught. C++ is one of the few languages that allows coopertive multiple inheritance. And also the only one that implements exceptions correctly, i.e. in conjunction with the destructor.

So if you want to only implement OO then stick with Java/C#. If you want to implement OO and mix in expression templates, DESLs, policy based design, kernel hacks, assembler, and interface directly with FORTRAN binaries via shared memory, then choose C++.

Lance Diduck
Of course the funny part is that C# and Java have practically nothing to do with OO as defined by the guy who coined the term, or the language he created around it. :)
jalf
+4  A: 

You started off asking "Why are C# and Java recommended more?" That was a really good question, with some potentially unexpected answers.

But then you asked, in essence, "What makes C# and Java better?" Which more than anything shows you should have waited to hear some answers to the first.

Ok, one possible reason for the predominance of recommendations of C# and Java is that they actually are best at OOP. Better than C++. Better than Haskell. Better than Smalltalk. This is likely NOT the correct reason.

A much more likely reason is that people recommend what they know. This suggests that a better follow-on question is: "Why do more people do object-oriented programming in C# and Java than in C++?" Now that's a good question that I can only provide a few of the answers to.

The biggest answer is "Because that's what most universities teach." But that begs the question. "Why do the universities teach C# and Java?" Two big factors.

The professors prefer Java and C# because the theoretical basis is much, much simpler than C++. The languages themselves have a lot more interesting theoretical properties which make proofs easier. The typing systems are much much simpler, which leaves room for expansion, for hey if we add these markers to the Java type system (e.g. taint indicators on user-supplied data) we can write a little proof of concept pre-compiler and publish some academic papers on our new Java flavor. With C++ the make your own flavor isn't so attractive. Partly because C++ is already so flexible, and partly because the prototype parser/type color thingamajig is now REALLY hard because it has to handle C++ syntax.

Universities also like Java and C# because they attract students. Students go for what looks best on their resume. What works best on the resume depends on the advertising propaganda the headhunters swallow from famous companies like -- you guessed it -- Sun and Microsoft and Novell.

Then, Java and C# are much easier to master than C++. Not necessarily easier to get any particular job done, although the standard libraries, garbage collection, and code refactoring tools (back to C++ parsing difficulties) certainly do help. But C++ has so much more capability than Java or C# that the number of true masters is much less, both in absolute numbers and percentagewise.

And, C++ masters are less likely to either program O-O or simply suggest their language for learning OOP, since they have so many paradigms to work with. They'll instead ask "Why are you learning OOP when generic programming is so much more powerful?"

So there, you have half a dozen counter-cultural reasons for the popularity of C# and Java as an answer to the question "What language is best to learn OOP?" that can't be summarized as "they're better, that's all".

Ben Voigt
C++ masters might also wish to prevent others from suffering like they have.
kibibu
+1  A: 

You can't do pointer manipulation in Java or C#.

Using C++ to understand OOP will require a lot more effort than doing so in Java or C# because the C++ class system is more complex, the language allows for full 'low level' access down to assembly level, the industry standards rely on libraries and conventions that take a lot of exposure to fully understand and the language wasn't designed to be easy for beginners in the first place.

10, 15 years ago there were no other (out-of-the-box) OOP capable languages with strong industry support, so C++ was pretty much the default choice for teaching OOP. These days we have Java on the JVM and C# on dotNet and while purists will tell you that "they're not really OO languages", they are good enough, have strong support and significantly lower the entry barrier to understanding and working with OOP concepts.

Michiel Kalkman
You absolutely can do pointer manipulation in C#. Read chapter 18 of the specification.
Eric Lippert
+2  A: 

While I think almost every possible reason for learning OOP with C# or Java has been covered here (pro and con):

I also think a major reason (and this relates to Ben Voigt's comment on his belief that Java and C# are the languages being taught in computer science classes) is sheer "size and momentum": knowing you have resources like StackOverFlow and CodeProject, and a critical mass of programmers some of whom will always be "pushing the envelope" on explaining how to use the latest "goodies" in the language ... makes a great deal of difference.

... as an example of "envelope pushers": I'm thinking of the WPF Disciples, for example, and the "great FrameWork/DI/IOC" softwares and the debates surrounding them which have become as mysterious to me as English Cricket ...

Note: I would like to see statistics on the extent to which C# or Java is used in intoductory college courses in the US and Europe: I think that could be very interesting. I would interpret Ben's comment as a very highly probable hypothesis, but still a hypothesis.

It's interesting that no one here has proposed Objective C as found on the Mac for learning OOP (speaking as a fully de-programmed former member of the "Cult of Mac").

I would also observe that a lot of the time when people are speaking of "OOP" they are speaking: not of O-O design; and, not of "design patterns," but speaking of programming with built in system objects ranging from utility libraries to GUI-thingies, and often speaking about the quirks and anomalies of getting them to work under particular conditions in particular environments.

Anyone who's sweated with the standard WinForms controls (ListView, TreeView, ComboBox, etc.) for a few yearas has, imho, "learned" a lot about inconsistency in object design: from the "glass is half-full perspective," however, maybe that's what ENABLES the third party software companies to thrive and innovate ? It has certainly motivated me to buy a 3rd. party set of tools that have much more "design consistency and integrity."

In summary : I'd say the best resources for learning OOP are a selected few language-dependent books (like, for C#, Skeet's masterpiece, "C# in Depth"). What book would you recommend for Java-OOP ? Hopefully, at best, using your key book(s) in the context of a course or mentoring. And, then StackOverFlow and CodeProject come into play as fantastic resources to help you in your own continuing self-education.

A comment: in the study of music you have the concept of "etudes" which not only help you (and force you) to master new technques and musical concepts, but also are immediately useful (in the aesthetic sense in music: i.e., pleasing to the ear).

imho the "great book" on C# "Design Patterns," using the idea of a set of graduated progresive exercises, as in musical etudes, and taking full advantage of language evolution in FrameWorks 3-4, is waiting to be written ... by Jon Skeet, or Eric Lippert, but I'd be just as happy to have one from Jesse Liberty or Matthew McDonald, or Richter, or Troelsen, or Sells.

Would it be valuable to distinguish here two types of "educational goals" : turning out competent application programmers whose area of expertise is platform specific (i.e., Mac, PC, Linux): and turning out "computer scientists" who are able to analyze problems using a number of methods of abstractions, who are focused on algorithms, and, perhaps secondarily, on the choice of which full-Turing equivalent language they feel best fits the implementation need of a particular algorithm ?

Or is there another "role" here of "application model architect" who dreams in UML, and operates on a level of abstraction far beyond "mere coding" :) ?

Meanwhile, fossils, like this writer, will continue to mess around with what they refer as "dinosaur dentistry" using the equivalent of duct tape and crazy-glue to make archaic levels of legacy software (often from competng vendors) work together, kind of.

BillW
Everything is a hypothesis, the only question is the quality of data militating for or against it. I have personal experience with Java CS courses at both the graduate and undergraduate level, although I didn't major in CS (not theoretical anyway, I guess you could call certain EE specialties applied CS). It is admittedly a small sample size, but evidence nonetheless. At the time C# was brand-new, and it takes some time to work its way through curriculum committees and the like, probably Java has lost some "classroom share" to C# by now.
Ben Voigt
@Ben I don't question your personal experience; but your personal experience is one data-point. I just would like to have more real data, from a wide variety of colleges and universities, community colleges, etc. on the extent to which C# and Java are used across the US and Europe in introductory computer science courses as of now. And knowing if there has been a shift from Java to C# would be very interesting, indeed. I suspect the data is "out there" somewhere.
BillW
Well, I can give you another datapoint. University of Copenhagen, Denmark, teaches Java in its first year "intro to OOP" class (although not as the first language -- that spot is reserved for SML). Students do run into some C++ later (in my case it was on second year, when we were given 2x1 hour labs to get a feel for the language before we had to write a simple OS kernel with it ;))C++ is also used quite a bit in later courses, probably even more so than Java. (But not as much as SML ;))As far as I know, there is no C# taught whatsoever.
jalf