File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public and private inheritance in Java like in C++?

 
Timothy Sam
Ranch Hand
Posts: 751
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although I don't understand the us of it, there's this thing called public inheritance in C++ that goes like this...



I've been hooked up with Java for quite sometime but never encountered this... I just thought of this when I was doing some reading with C++. Anyone? Thanks!
[ March 23, 2006: Message edited by: Timothy Sam ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java only has public inheritance. That is, you both inherit implementation and interface.

With private inheritance, you would only inherit implementation. It's basically a more static form of composition, as far as I remember.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, when comparing Java to C++ we are talking analogies, but public derivation in C++ is analogous to Java's sole inheritance mechanism. Where it gets funky is with other forms of inheritance in C++, like private:

class Dog : private Animal {...}

This is closest to Dog having a private Animal field:


Because the C++Dog is derived from Animal it can access its protected animal parts (dogs do that, eh), so the match isn't perfect. There is also 1 piece of C++ syntax that I can't recall the details of, perhaps it is written:

public: Animal::foo;

That lets you reexpose Animal methods in Dog that would otherwise be hidden. In Java you delegate:

public void scratch() {sooper.scratch();}

There is also protected derivation:

class Dog : protected Animal

which is closest to making that sooper field being protected instead of private.

Again, this is all a rough analogy. Perhaps the crux of the matter is which language is better served by its derivation mechanism. In C++ private derivation ("implementation derivation") is praised for being more efficient than using delegation. In Java those sorts of concerns seem to be passed off to hot-spot optimization.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic