I have to say "good" Java design is almost uniformly terrible. I've never seen so much code duplication, ridiculous layered levels of abstraction (but almost never the abstractions that would have actually made sense in the situation) as when looking at Java code.
There are many good features of C++ that Java has no equivalent for.
Features that enable cleaner, more robust and more elegant designs.
C++ programmers should code like C++ programmers. For two very good reasons:
- They have to respect the weaknesses and flaws of the language. Trying to pretend you're in a garbage-collected language when you're not is a recipe for disaster. And trying to implement GC-like semantics on top of a language like C++ is probably even worse.
- And just as importantly, they should exploit every strength of the language. When you can get truly generic collection classes with zero overhead and a sophisticated iterator implementation, why on Earth would you throw it away? When you've got one single sorting function that works for any container and even more generally, for any sequence of objects of any type, why would you throw it away?
Since you mention interfaces as an example of a feature C++ programmers should emulate, there are two important counterpoints:
- C++ has interfaces in the form of abstract classes. The semantics are slightly different, but they can be used to serve the same purpose.
- C++ doesn't need interfaces as much, because it has templates and concepts relying on compile-time ducktyping. Rather than having every iterator derive from an
IIterator
interface, we can simply define how an "iterator" should behave, and write a class that supplies members with the same names. As long as it "looks like" an iterator, it can be used as an iterator. Template metaprogramming tricks even make it possible to adapt existing classes to "retrofit" them to support a concept they weren't designed for. For example, raw pointers (as taken from C) work as perfectly valid iterators, despite missing a few typedef
members. And without even being classes in the first place.
Of course, C++ has plenty of weaknesses too. But it is not simply an "inferior Java". It is a different language. There is no need for C++ programmers to emulate Java features.
As I read in Thinking in java
Java books are rarely good sources of information about C++. ;)