Re: How to... private inheritance ?!



sakis.panou@xxxxxxxxxxxxxx wrote:
In C++ one can create an interface through a pure abstract class then
derive from that interface privately, whilst providing concrete
implementation of the methods exposed by the interface. The reason one
might want to implement a private inheritance is to force the user of
the concrete class to cast it to the interface without allowing them
to access the interface's methods through the concrete class itself.

I've not done enough C++ programming to say, and I don't have my /Effective C++/ book on me, but my recollections on private inheritance is designed for do not match your description. My recollection is that private inheritance models an "is-implemented-in-terms-of" relationship, which, following the concept of information hiding, should naturally be private.

The idea have having pure interfaces that are implemented via private inheritance sounds... hacky to me.

Car theCar;
IWindow* pCarWindow = reinterpret_cast<IWindow*>(&theCar);

I may be misremembering my C++, but this is an *extremely* dangerous cast, one which would probably fail if you had multiple inheritance.

I am fully expecting some folk not to like the idea of private
inheritance in C++ and that is more than fair, but it works really
well especially when you really want to enforce the users of the
concrete class not to use the interfaces's methods directly from the
concrete class's instance.

The use of private inheritance is to hide implementation details--the same reason you want to use private variables in general. Since you're using reinterpret_cast instead of static_cast, you're explicitly subverting the type system in this instance. I have no qualms about using private inheritance, so long as it is used correctly.

So my question is how do I achieve the same in Java ? Any help of
suggestions would be truly appreciated.

As is suggested in /Effective Java/ (and in /Effective C++/ (more or less) for that matter), "prefer composition to inheritance." The norm is generally to have a private nested class, which may or may not be static, depending on circumstances.

--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.



Relevant Pages

  • Re: How to... private inheritance ?!
    ... In C++ one can create an interface through a pure abstract class then ... Private inheritance doesn't get you that, though: public inheritance with private overrides does. ... virtual void foo { ...
    (comp.lang.java.programmer)
  • Re: How to... private inheritance ?!
    ... implementation of the methods exposed by the interface. ... might want to implement a private inheritance is to force the user of ... to access the interface's methods through the concrete class itself. ... void openWindow(void) = 0; ...
    (comp.lang.java.programmer)
  • Re: How to... private inheritance ?!
    ... implementation of the methods exposed by the interface. ... might want to implement a private inheritance is to force the user of ... to access the interface's methods through the concrete class itself. ... and preventing error. ...
    (comp.lang.java.programmer)
  • How to... private inheritance ?!
    ... In C++ one can create an interface through a pure abstract class then ... might want to implement a private inheritance is to force the user of ... to access the interface's methods through the concrete class itself. ... void openWindow(void) = 0; ...
    (comp.lang.java.programmer)
  • Re: basic question on C++
    ... |>On a job interview question, ... | All I can think of is that "how do you set variables in the base class ... | to be all private" is meant as if it were followed with "...to the ... That would be via private inheritance: ...
    (alt.comp.lang.learn.c-cpp)