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

 
thomas davis
Ranch Hand
Posts: 207
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike C++, Java is not supporting multiple inheritance .I would like to know more about this feature provided by C++ and the reason why Java is not supporting Multiple Inheritance.
And paradoxically, if I am not wrong Java supports multiple inheritance with the help of interfaces and it called as multiple implementation inheritance. Please correct me if I am wrong.
1) How does it possible?
2) How to go about this feature?
3) How to implement this in our application?
4) Suppose If I extend more than one Interface or implement more than one interface, how to call appropriate variables and methods from corresponding interfaces in a class which implements these interfaces.
5)What is multiple implementation inheritance
Please give some insights on multiple inheritance in JAVA.
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple inheritance in C++ caused a lot of confusion and errors, and the designers of Java decided not to support it. Instead, in Java you extend one class and implement unlimited number of interfaces. For example:

I am sure you will get a ton of links if you search the Java Beginner and Java Intermidiate forums, -- this is such a common question.
Outside of JavaRanch, a good old Designing With Interfaces article from javaworld may help you understand the concept of the interfaces in Java.
Eugene.
[ April 28, 2003: Message edited by: Eugene Kononov ]
 
thomas davis
Ranch Hand
Posts: 207
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused with methods in the interfaces.
Suppose if I have a interface called IntfA with method runMethod() and at the same time if I have another method with the same name ie: runMethod() in some other interface called intfB.
And If I implement these interfaces on a class,how can I implement this methods in that particular class?
class MyClasss implements IntfA ,intfB
{

MyClasss() {};
/*
I am implementing the method in the IntfA
*/
public void runMethod()
{
System.out.println("run");
}
/*
I am implementing the method in the IntfB
*/
public void runMethod()
{
System.out.println("WALK");
}

}
Am I violating the rules provided by JAVA API?,If so ,how can I implement this feature when two interfaces have same method?
:roll:
 
Andrea Hill
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because interfaces only contain the method definition and not the implementation, it is ok if both interfaces have a method of the same name (though not ideal). But in your class you can only write one implementation -- so the code above is incorrect. It's pretty straight-forward if you think about it: the interface tells you what methods your code needs, without telling you HOW to accomplish what you need to. In your class you implement the methods as needed, and there is no reason why you would want two methods with the same name to do two different things. How would you know which to use? This is also in part why you can't extend two classes - in the case there were two different implementations of a method of the same name in each class. By limiting multiple inheritance, java makes it easier to ensure classes don't contain too much ambiguity.
 
thomas davis
Ranch Hand
Posts: 207
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it a DIAMOND PROBLEM ?If not what is the DIAMOND PROBLEM?I read one of the articles in JAVAWORLD saying that "java diamonds prblem are forever" Is that true?
How does java solve the multiple-inheritance diamond problem?
How does it differentiate between inheritance of implementation and inheritance of interface?
In class we are implementing the methods,how this is goig to create problem while inheriting from more than one calss?
If I extend more than one class ,what could be the problem?
How this problem solved by Interfaces?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem becomes more noticeable if the two interfaces have methods that aren't completely the same.
Look at this:

It is impossible to create a class that can implement both interfaces. C# has a way to implement this but Java does not.
 
Wilson Mui
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Paul,
What does happen in a situation like you described, when a class does implement both and have to actually define the method.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C# the method defintion gets the name of the interface added to it in the implementing class. So I would code:
void A.method1() {} //implicitly public
public bool method1() {return true;}
The second one is the default because it doesn't have a qualifier.
Which one gets invoked depends on how it is invoked. Let's say class C implements A and B:
A a = new C();
a.method1(); // invokes first one because it is invoked with an A.
B b = new C();
b.method1(); //invokes second one
C c = new C();
c.method1(); // also invokes second one because it it the default
[ April 30, 2003: Message edited by: Thomas Paul ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic