• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance  RSS feed

 
Vimlesh Kumar Patel
Greenhorn
Posts: 20
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello There,

I studied that java does not support multiple inheritance using classes. It is fine practically, but I have still a question in mind:

Lets see the below code.


Here, as we know that that Object is Super class for every class in java. and I have already extends a Test class in MyTest.
My question is: How is it possible to extend features of two class at the same time?

Theoretically,

MyTest extends Object &
MyTest extends Test.


Thanks.
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vimlesh Kumar Patel,

How is it possible to extend features of two class at the same time?

Actually it inherits from the direct superclass, and the direct superclass inherits from its direct superclass.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can implement any number of interfaces but extend only one class.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple inheritance is something like this:Java does not support that.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is does support is this:




Which protects against the dangerous diamond...
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To help make this clear for you we are correct when saying Java does not utilize multiple inheritance, and this is for a good reason. If a language is allowed to use multiple inheritance then you may encounter something known as the Deadly Diamond of Death. Following is a brief example showing you exactly what this is.

Imagine a hierarchy of Canines. The superclass is Canine and this superclass has two direct subclasses. One subclass is Labrador Retriever and the other is a subclass Poodle. Both Labrador Retriever and Poodle share a subclass Labradoodle. In addition, Canine holds a method bark() that both Labrador Retriever and Poodle inherit. And if Labradoodle extends Labrador Retriever and Poodle, Labradoodle must also inherit the bark() method. Also, just for clarity, when a Labrador Retriever and a Poodle breed in the real-world you are left with what is called a Labradoodle.

Here is the illegal code that would show this hierarchy:



Take a good hard look at that code and figure out what is wrong...

To start, the class Labradoodle cannot extend two classes, this is illegal. You can only extend once in Java, but you can implement as many times as needed.

The next thing is to ask yourself the question, "Which bark() method does Labradoodle inherit?" This is where the DDOD comes into play. Which method do we use? The JVM does not know which bark() method to use if you ever call it on a Labradoodle object so instead Java will make your programming life easier by not allowing multiple inheritance. If we allowed the DDOD, your program would most certainly explode at run-time! So, without multiple inheritance functionality in a language we can say good bye to the DDOD.

Your question is that since everything is a subclass of Object how can we extend a class that is not Object and also be extending Object. The answer is that we are not!

In your code example you are saying something more like:



Which is illegal, just like the Canine example. The real way we get class Object in this hierarchy is like this.



So you can see that the MyTest class does not extend class Object but rather it inherits the methods from class Test which also contains methods from class Object. You can see this by actually using methods declared in the API for class Object. Here I use the method getClass().



you should compile this and run it. Test yourself by figuring out why this works! Good luck!
 
Vimlesh Kumar Patel
Greenhorn
Posts: 20
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again!!! CodeRanch Team
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
Also, thanks for the Cow!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!