Re: How to... private inheritance ?!
- From: Joshua Cranmer <Pidgeot18@xxxxxxxxxxxxxxx>
- Date: Tue, 03 Mar 2009 21:33:21 -0500
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
.
- References:
- How to... private inheritance ?!
- From: sakis . panou
- How to... private inheritance ?!
- Prev by Date: Re: How to... private inheritance ?!
- Next by Date: Re: Java in Browser
- Previous by thread: Re: How to... private inheritance ?!
- Next by thread: Re: How to... private inheritance ?!
- Index(es):
Relevant Pages
|