File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Multiple Inheritance in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multiple Inheritance in java" Watch "Multiple Inheritance in java" New topic

Multiple Inheritance in java

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
How do I achieve Multiple Inheritance.
Suppose I have class A and B. Both have a method called 'show' that has same type and signature in both the classes. Now I want to have a class X which has both the 'show' methods that are implemented in A and B.
Thanks in Advance.
maateen ashraf
Ranch Hand

Joined: Jan 08, 2001
Posts: 122
one procedure u can follow is to develop an inteface for class A or B
just implement ur class from both interfaces & provide the
method defination for both methods from both interfaces..
then u can call them accordingly..
hope it would help U....
maateen ashraf
Ranch Hand

Joined: Jan 08, 2001
Posts: 122
just define Inner Class in Interface &
implement that Interface to ur class...
A utility inner class will be used by all classes, which implement the interface. It would be a way of cheating on no-multi-inheritance in Java and actually made the interface fully implemented, provided the programmer is tricky enough. Interesting stuff, isn't it?
I can even think of a situation in which it might be useful.
i Think that would help U....
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
your reply is quite confusing atleast to me....
just implement ur class from both interfaces & provide the
interfaces are implemented not classes.......maybe the abv sentence should read implement your interfaces from classes
any comments .........
- satya

Take a Minute, Donate an Hour, Change a Life
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
Let's put it another way.
You can't
Each class can only have a single method with a given name and argument signature.
So, you can either:
a) Have two 'show' functions, with an extra argument to indicate which variant of 'show' is being invoked, and/or
b) have a single incarnation of the method, and somehow try to decide what to do with it.
I know neither choice is what you were after, but I believe (ie. I could be wrong here) that these sorts of situations are extremely messy to deal with, and was part of the reason the Java originators decided not to implement multiple-inheritance in the first place.
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
While you're on the subject, if class SomeClass extends a class with show() (for example) and implements an interface with show(), which would be called when was invoked?
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Interesting qstn......
But praatically do you think this has any
use at all!
Anyways, if you implement the show() interface,
you are overwriting the show() method from the
parent. Hence, the show() method that you implemented
is the one which is executed.
On the contrary, you could do this also:
- donot implement the interface explecitly. It is
assumed that the interface is implemented since, the
parent class implements it. I din't think this would
work till I ran....was very interesting though....
chk this out.....

- satya
Vipin Dube

Joined: Feb 02, 2001
Posts: 5
Simple answer to your question is that Java does not support Multiple inheritance in class hierarchy. Only Multiple interface inheritance is allowed. ok.

I agree. Here's the link:
subject: Multiple Inheritance in java
It's not a secret anymore!