If one class is derived from another that is deriv

2020-05-09 22:38发布

The fact about Java is that it does not support the multiple inheritance.

But I have a question that the base class of all java classes is Object.

Now we have two classes : Class A and Class B. B is inherited from A then the Base class of B would be the Object and A , so here, the multiple inheritance took place.

Anyone will please help me to clear my doubt?

7条回答
forever°为你锁心
2楼-- · 2020-05-09 22:56

What you describe is not multiple inheritance; B inherits from a single super class, A and A inherits from a single super class, Object. B gains the properties and methods of both A and Object, but this is via a single 'chain'of inheritance.

Multiple inheritance is where a class inherits directly from more than one, unrelated class. This is not possible in Java.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-05-09 22:57

A class always extends only one class. That is the Object class or the class defined with the extends keyword. That class in turn can extend also only one class until eventual the Object class is reached.

查看更多
Ridiculous、
4楼-- · 2020-05-09 22:59

A, and B inherits Object so A inherits Object when inherits B.

查看更多
够拽才男人
5楼-- · 2020-05-09 23:11

The concept of Multiple Inheritance means, that you can have a class A, B and C where A is derived from B and C like this:

class A extends B, C {}

This is not possible in Java.

What you describe is a straightforwar inheritance with a direct line of descendenats.

class A {};
class B extends A {}
class C extends B {}

You don't really need Multiple Inmheritance though, because with Interfaces you can basically achieve the same in a much cleaner way.

查看更多
小情绪 Triste *
6楼-- · 2020-05-09 23:20

Object class is base class of all other classes.Here when you inherit class B from class A then class B can not be inherit from Object class.

public class B extends class A
{

}

And here, base class of class A is Object class.

So in short, if we not inherit any class then its base class would be object class.

You can also refer this:

http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html

查看更多
家丑人穷心不美
7楼-- · 2020-05-09 23:21

Think of inheritance as an "is a" relation.

In Java we can have

A dog is a mammal is an animal

but not

A dog is a mamal and a four legged animal

mammal and four leggeld animal are at the same level but mammal and animal are at different levels.

The reason we can have the first but not the second is if we know that mammals talk in a certain way, animals talk in a certain way and four legged animals talk in a certain way we can work out the way dogs talk unambiguously in the first case but not the second.

查看更多
登录 后发表回答