问题
These two facts in java
Fact 1
“Every class in java by default extends the java meta class Object
”
and
Fact 2
“Multiple inheritance is not allowed in java” read more about diamond proble here Java inheritance
are quiet confusing
Suppose ClassB
extends ClassA
then according to fact1 ClassB
extends Object
Does that mean ClassB
is extending both ClassA
and Object
? Is it a case of multiple inheritance?
If it’s not multiple inheritance then how aren't the two statements contradictory?
回答1:
“Every class in java by default extends the java meta class Object” //fact1
Every Class extends Object
class, only if they don't extend any other class directly.
If a class Test
extends another Sample
class, then Test
don't extends Object
class directly, but, inheriting the Object class behaviors through super class Sample
, which directly extends Object
class.
回答2:
Thanks for providing the source of your facts. Now I can go into a bit of detail.
"Head First Java 2nd edition" says on page 208:
Every class in Java extends Object
Class Object is the mother of all classes; it's the superclass of everything.
Further down in the paragraph it says:
Every class you write extends Object, without you ever having to say it. [...]can think of it as though a class you write looks like this:
public class Dog
extends Object
{ }
[...] Dog already extends something, Canine. [...] The compiler will make Canine extend Object instead.
While the general idea that they're trying to communicate (that every class in Java has java.lang.Object
as a superclass) is correct, they are using the wrong terminology, and the example using Dog
is plain wrong and that is leading to your confusion.
I've written a Java compiler (for Java 1.4) and I'm pretty familiar with the Java Language Specification, so bear with me.
To prove that the terminology is "Head First Java" is wrong I need to quote from the Java Language Specification, which is a bit technical.
But to start with I can give you an easier explanation how you can see this.
Better definition
Every class in Java that does not
extend
another class explicitly, extendsObject
When you write:
class Canine { }
Then the compiler takes this to mean:
class Canine extends Object { }
But when you write:
class Dog extends Canine { }
Then the compiler sees that you have already explicitly extended a class, and doesn't change the meaning of your code.
Since Head First Java second edition is based on Java 5, I'll use the Java Language Specification for that version of Java.
The meaning of extends is defined in section 8.1.4 of the JLS:
8.1.4 Superclasses and Subclasses
The optional extends clause in a normal class declaration specifies the direct superclass of the current class.
Super: extends ClassType
As you see, extends only refers to a direct superclass. In the example in Head First Java, Dog doesn't extend Object, because its direct superclass is Canine. Only Canine extends Object.
What they meant to say is that Object is a superclass of all other classes in Java. This is defined in JLS section 4.3.2:
4.3.2 The Class Object
The class
Object
is a superclass (§8.1) of all other classes. A variable of typeObject
can hold a reference to thenull
reference or to any object, whether it is an instance of a class or an array (§10). All class and array types inherit the methods of classObject
, [...]
I'm afraid that you got sidetracked by the misleading way that this was presented in the 2nd edition of Head First Java. Hopefully that have already fixed this in newer editions (if anyone has one, please confirm/refute), otherwise we should inform the authors.
回答3:
Yes, both the statements are correct.
It’s true that every class in java extends the meta class Object
.
Still ClassB doesn’t have multiple inheritance because at compile time java shifts the Object
Class by one level.
This way now ClassA
now extends Object
and not ClassB
.
This way ClassB
is not having multiple inheritance so fact2 is followed.
Now ClassB
extends ClassA
and ClassA
extends Object
thus through MultiLEVEL Inheritance ClassB extends Object
; fact1 followed
来源:https://stackoverflow.com/questions/22881645/every-class-in-java-extends-the-metaclass-object-does-it-imply-that-every-subc