问题
While i was reading java book, i came across "Every class extends class Object"...but if want a class B to extend class A.....but Class B will be now having multiple inheritance,one from Object class and one from Class A.How the conflict is resolved. Can anyone explain?
回答1:
First of all, Object class is the super/base/parent class of every class including user-defined classes.
So even if we don't mention it explicitly, the user-defined classes extends Object class by default.
Morevoer, Object class implements a set of methods and variables which are common to all the objects being created in the application. This is the main reason why we have Object class as a base class for all the other classes.
For eg:
hashCode() - this method creates a unique identity for each of the object being created in JVM.
回答2:
Its multi-level inheritance, not multiple:
class A extends Object
class B extends A
回答3:
ClassB extends from ClassA which also extends from Object. Therefore ClassB extends Object indirectly, through classA
"Every class extends class Object" just means if you don't specify the parent class, it takes on Object as the parent class
回答4:
The book was trying to explain that every class is either a direct or indirect subclass of Object. Among other things, this means that every class inherits the public methods of Object: toString(), hashcode(), wait(), etc. It also means that whatever class variable a happens to be, you can always assign a to a variable of class Object.
There is no such thing as multiple inheritance in Java. The closest Java comes to that is interfaces, which is a whole subject in itself.
回答5:
there is no conflict.. take a look at this structure
- animal
- bird
- sparrow
- parrot
- dog
- poodle
- cat
- bird
the parrot class gets all attributes/methods of its super-class bird and from its super-class animal. this is called multiple inheritance.
You get traits from your parents right? You also get traits from their parents also.
来源:https://stackoverflow.com/questions/8470535/inheritance-of-object-class-in-java