For Programmers: Free Programming Magazines  


Home > Archive > Java Help > June 2005 > Coming to java from C++: multiple inheritance









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Coming to java from C++: multiple inheritance
john

2005-06-09, 8:58 am

Hello,

In C++ it is easy to have an object with the function of two classes by
inheriting from them:

class View { ... }

class Window { ... }

class Form: public Window, public View { ... }

How can I do this in java ?

Using interfaces requires to re-implement functions in FormView which
are already implemented in class Window or in class View.

Thanks,
John.







dannie

2005-06-09, 8:58 am

Well, there is inheritance (keyword extends) but I think that a class
can extend only 1 class (which can extend another but not two "at the
same time")... but I'm not sure so you can check...

john ha scritto:
> Hello,
>
> In C++ it is easy to have an object with the function of two classes by
> inheriting from them:
>
> class View { ... }
>
> class Window { ... }
>
> class Form: public Window, public View { ... }
>
> How can I do this in java ?
>
> Using interfaces requires to re-implement functions in FormView which
> are already implemented in class Window or in class View.
>
> Thanks,
> John.


Matt Humphrey

2005-06-09, 8:58 am


"dannie" <daniela.sibona@gmail.com> wrote in message
news:1118310834.544963.250460@f14g2000cwb.googlegroups.com...[color=darkred]
> Well, there is inheritance (keyword extends) but I think that a class
> can extend only 1 class (which can extend another but not two "at the
> same time")... but I'm not sure so you can check...
>
> john ha scritto:

There is no automatic way in Java to inherit functionality directly from
more than one classe. Java multiple inheritance is via interfaces, so you
can make you class inherit from multiple interfaces. You don't necessarily
have to reimplement the functionality because your new class can still be a
composition of the other classes. Your class must simply redirect to the
compositions.

interface IView
class View implments IView
interface IWindow
class Window implements IWindow

class Form implements IView, IWindow {
View view, Window window;

methods for IView, IWindow are redirected to the components
}

Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/



Hal Rosser

2005-06-11, 3:58 am

Pehaps you may consider creating an instance variable of the 'other' class.
This approach is preferred in many cases. Composition is a good alternative
to multiple inheritance, and is preferred by some authors of the subject.
HTH


"john" <personne@microsoft.ccom> wrote in message
news:42a7ef6a$0$3113$8fcfb975@news.wanadoo.fr...
> Hello,
>
> In C++ it is easy to have an object with the function of two classes by
> inheriting from them:
>
> class View { ... }
>
> class Window { ... }
>
> class Form: public Window, public View { ... }
>
> How can I do this in java ?
>
> Using interfaces requires to re-implement functions in FormView which
> are already implemented in class Window or in class View.
>
> Thanks,
> John.
>
>
>
>
>
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com