开发者

Java Doubt on Multiple inheritance

Java does not support multiple inheritance but Object class is by default super class of all classes. e.g

class Object
{

}
class B
{

}
class A extends B
{
}

Class A can access all method of开发者_JAVA百科 B and Object.Is not it a example of multiple inheritance? So is it correct that Java does not support multiple inheritance.


my question is not to find difference between multilevel and multiple inheritance. Java Docs,it self says :Class Object is the root of the class hierarchy. Every class has Object as a super class. All objects, including arrays, implement the methods of this class. So it means Class Object is super class of Class A{Previous example}. But Class B is also Super Class of Class A. SO what is meaning of it ?


It is know as transitive nature of Inheritance.


Look at the difference between transitive inheritance (C inherits directly from B and transitively from A):

transitive

and multiple inheritance (C inheriting from both A and B):

transitive


No it isn't. Multiple inheritance is when a class has more than one direct base class, as in:

class A {}

class B {}

// not valid Java
class C extends A, B {}

A class may have many indirect base classes, each with only one direct base class, as in:

class D extends A {}

class E extends D {}

class F extends E {}

Here the inheritance hierarchy is F -> E -> D -> A -> Object, but this is still single inheritance.


Since no Java class can extend directly two or more classes you can safely say that Java does not support multiple inheritance.

You would have multiple inheritance if you were able to say class A extends B, C, but you can never do that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜