开发者

How can a class, extending another class, extends Object class though multiple inheritance is not allowed?

开发者 https://www.devze.com 2023-03-25 20:38 出处:网络
Since I am new to java. I want to know if multiple ineritance is not sup开发者_开发知识库ported in java then how a classextends another class alongwith the default superclass Object?Because although m

Since I am new to java. I want to know if multiple ineritance is not sup开发者_开发知识库ported in java then how a class extends another class alongwith the default superclass Object?


Because although multiple inheritance isn't allowed, one class can inherit from another which can inherit from another - and eventually the class at the top of that chain will inherit from object (it'll do that if you don't specify any specific class for it to inherit from.)


Although this is already answered, here is a diferent perspective. Try to think of it in human terms. You can't have 2 biological fathers, but you inherit the traits from your father, your grandfather, great grandfather and so on... In the same way, when you extend a class, that class becomes the parent class and you'll inherit traits from every parent class up the tree.

;)


There are two similar sounding concepts related to inheritance Multiple Inheritance and Multi-Level Inheritance.

Multiple Inheritance is not allowed in java. This stops a class from inheriting multiple classes. For example we can't declare a class as:

Class C extends A, C

But as multilevel inheritance is allowed, extending of class B, which extends class A, by class C is allowed. So class hierarchies like

Class B extends A

and

Class C extends B

is allowed.


"Multiple inheritance" is different than what you describe - it refers to a single class extending more than one class, such as

public class MultipleClass extends ClassA, ClassB

What you have described is just a hierarchy of inheritance.

0

精彩评论

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

关注公众号