如果一个类派生自另一个派生自Object的类,那就是“多重继承”

时间:2022-09-25 10:10:29

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

关于Java的事实是它不支持多重继承。

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

但我有一个问题,即所有java类的基类都是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.

现在我们有两个类:A类和B类.B继承自A然后B的Base类将是Object和A,所以这里发生了多重继承。

Anyone will please help me to clear my doubt?

有人会帮我清除疑虑吗?

7 个解决方案

#1


1  

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:

多重继承的概念意味着你可以拥有一个A,B和C类,其中A是从B和C派生的,如下所示:

class A extends B, C {}

This is not possible in Java.

这在Java中是不可能的。

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

你所描述的是直接的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.

但是,您并不需要多次Inmheritance,因为使用Interfaces,您基本上可以以更清洁的方式实现相同的目标。

#2


1  

class B inherits from class A which in turn inherits from Object. Object is at top of the inheritance hierarchy. This is multi level inheritance , not multiple inheritance.

B类继承自A类,而A类继承自Object。对象位于继承层次结构的顶部。这是多级继承,而不是多重继承。

#3


0  

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

A和B继承Object,因此A在继承B时继承Object。

#4


0  

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.

一个类总是只扩展一个类。这是Object类或使用extends关键字定义的类。该类反过来也可以只扩展一个类,直到最终到达Object类。

#5


0  

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.

你描述的不是多重继承; B继承自单个超类,A和A继承自单个超类Object。 B获得A和Object的属性和方法,但这是通过单个“链”继承。

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

多重继承是一个类直接从多个不相关的类继承的地方。这在Java中是不可能的。

#6


0  

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.

Object类是所有其他类的基类。当您从A类继承B类时,B类不能从Object类继承。

public class B extends class A
{

}

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

在这里,A类的基类是Object类。

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


0  

Think of inheritance as an "is a" relation.

将继承视为“是一种”关系。

In Java we can have

在Java中我们可以拥有

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.

哺乳动物和四种leggeld动物处于同一水平,但哺乳动物和动物处于不同的水平。

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.

我们可以拥有第一个但不是第二个的原因是,如果我们知道哺乳动物以某种方式说话,动物以某种方式说话,并且四条腿的动物以某种方式说话我们可以按照第一种方式明确说话案件,但不是第二个。

#1


1  

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:

多重继承的概念意味着你可以拥有一个A,B和C类,其中A是从B和C派生的,如下所示:

class A extends B, C {}

This is not possible in Java.

这在Java中是不可能的。

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

你所描述的是直接的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.

但是,您并不需要多次Inmheritance,因为使用Interfaces,您基本上可以以更清洁的方式实现相同的目标。

#2


1  

class B inherits from class A which in turn inherits from Object. Object is at top of the inheritance hierarchy. This is multi level inheritance , not multiple inheritance.

B类继承自A类,而A类继承自Object。对象位于继承层次结构的顶部。这是多级继承,而不是多重继承。

#3


0  

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

A和B继承Object,因此A在继承B时继承Object。

#4


0  

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.

一个类总是只扩展一个类。这是Object类或使用extends关键字定义的类。该类反过来也可以只扩展一个类,直到最终到达Object类。

#5


0  

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.

你描述的不是多重继承; B继承自单个超类,A和A继承自单个超类Object。 B获得A和Object的属性和方法,但这是通过单个“链”继承。

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

多重继承是一个类直接从多个不相关的类继承的地方。这在Java中是不可能的。

#6


0  

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.

Object类是所有其他类的基类。当您从A类继承B类时,B类不能从Object类继承。

public class B extends class A
{

}

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

在这里,A类的基类是Object类。

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


0  

Think of inheritance as an "is a" relation.

将继承视为“是一种”关系。

In Java we can have

在Java中我们可以拥有

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.

哺乳动物和四种leggeld动物处于同一水平,但哺乳动物和动物处于不同的水平。

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.

我们可以拥有第一个但不是第二个的原因是,如果我们知道哺乳动物以某种方式说话,动物以某种方式说话,并且四条腿的动物以某种方式说话我们可以按照第一种方式明确说话案件,但不是第二个。