“every class in java extends the MetaClass Object” does it imply that every subclass causes a Diamond issue

后端 未结 3 551
鱼传尺愫
鱼传尺愫 2020-12-18 16:23

These two facts in java

Fact 1

Every class in java by default extends the java meta class Object

and

Fact

相关标签:
3条回答
  • 2020-12-18 16:33

    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.

    enter image description here

    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

    0 讨论(0)
  • 2020-12-18 16:42

    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 Dogextends 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, extends Object

    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 type Object can hold a reference to the null 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 class Object, [...]

    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.

    0 讨论(0)
  • 2020-12-18 16:54

    “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.

    0 讨论(0)
提交回复
热议问题