aspose file tools*
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
Author

MULTIPLE INHERITANCE IN JAVA

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
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

Joined: Oct 08, 2001
Posts: 2937
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

Joined: Feb 01, 2003
Posts: 207
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

Joined: Apr 22, 2003
Posts: 1
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

Joined: Feb 01, 2003
Posts: 207
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Wilson Mui
Ranch Hand

Joined: Apr 09, 2003
Posts: 140
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

Joined: May 05, 2000
Posts: 13974
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MULTIPLE INHERITANCE IN JAVA