Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

Coming to java from C++: multiple inheritance

Thread view: 
john - 09 Jun 2005 08:27 GMT
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 - 09 Jun 2005 10:53 GMT
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,
>
[quoted text clipped - 14 lines]
> Thanks,
> John.
Matt Humphrey - 09 Jun 2005 11:33 GMT
> 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
[quoted text clipped - 16 lines]
> > Using interfaces requires to re-implement functions in FormView which
> > are already implemented in class Window or in class View.

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 - 11 Jun 2005 03:35 GMT
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

> Hello,
>
[quoted text clipped - 14 lines]
> Thanks,
> John.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.