File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance in java

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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.
Vicha
 
maateen ashraf
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one procedure u can follow is to develop an inteface for class A or B
then
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
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maateen:
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 .........
regds.
- satya
 
Rob Acraman
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 SomeClass.show() was invoked?
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Grant:
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.....

regds.
- satya
 
Vipin Dube
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic