S
sakis.panou
Hi all,
Apologies if the subject has been already discussed but I have been
unable to find what I am looking for. My background is predominantly C+
+ and while I have been trying to familiarise myself with Java I have
come across something I can't get my head around.
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.
Here is an example of how this works in C++:
class IWindow
{
public:
void openWindow( void ) = 0;
void closeWindow( void ) = 0;
}
class Car: IWindow
{
private:
void openWindow( void ) {};
void closeWindow( void ) {};
}
Car theCar;
IWindow* pCarWindow = reinterpret_cast<IWindow*>(&theCar);
pCarWindow->openWindow();
pCarWindow->closeWindow();
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.
So my question is how do I achieve the same in Java ? Any help of
suggestions would be truly appreciated.
Kind Regards,
Sakis
Apologies if the subject has been already discussed but I have been
unable to find what I am looking for. My background is predominantly C+
+ and while I have been trying to familiarise myself with Java I have
come across something I can't get my head around.
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.
Here is an example of how this works in C++:
class IWindow
{
public:
void openWindow( void ) = 0;
void closeWindow( void ) = 0;
}
class Car: IWindow
{
private:
void openWindow( void ) {};
void closeWindow( void ) {};
}
Car theCar;
IWindow* pCarWindow = reinterpret_cast<IWindow*>(&theCar);
pCarWindow->openWindow();
pCarWindow->closeWindow();
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.
So my question is how do I achieve the same in Java ? Any help of
suggestions would be truly appreciated.
Kind Regards,
Sakis