Relationship between a subclass and its superclass

(1) A subclass inherits all variables and (normal) methods from its superclass:

 

Relationship between a subclass and its superclass

(2) A subclass do not inherit any constructor method from its superclass:

 

Relationship between a subclass and its superclass

Notice that a subclass object (always) contains a superclass object:

Recall:   objects are initialized using its constructor

Relationship between a subclass and its superclass

(3) A constructor in the subclass must invoke some constructor in its superclass as its first statement:

The keyword super( ... ) is used to invoke a constructor in its superclass

Demo:    using super(...) to invoke constructor in superclass

public class myProg
{
    public static void main()
    {
        NewClass a = new NewClass();
        NewClass b = new NewClass(44);
    }
}






  
public class NewClass
       extends SomeClass
{
    public int x;

    public NewClass()
    {
      super(); //Not:SomeClass()
    }
    
    public NewClass(int a)
    {
      super(a); //Not:SomeClass(a)
    }
}
  
public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }
    
    public SomeClass(int a)
    {
        x = a;
    }
}
  

DEMO: trace the execution in BlueJ

Relationship between a subclass and its superclass

(4a) If a constructor in the subclass does NOT invoke any constructor in its superclass as its first statement:

Then.... (can you guess what will happen ??? Remember the rules about default constructors ? )

Relationship between a subclass and its superclass

(4b) The Java compiler will automatically insert the call super( ) as the first statement:

 

Demo:    shows Java compiler inserts super( ) inside NewClass( )

public class myProg
{
    public static void main()
    {
        NewClass a = new NewClass();
    }
}






  
public class NewClass
       extends SomeClass
{
    public int x;

    public NewClass()
    {
      // Java adds super()
    }
    
    public NewClass(int a)
    {
      super(a); 
    }
}
  
public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }
    
    public SomeClass(int a)
    {
        x = a;
    }
}
  

DEMO: trace the execution in BlueJ

Quiz:    can you explain the error in this program ?

Explain the compile error in this program:

public class NewClass
       extends SomeClass
{
    NewClass()
    {
       // error  
    }
} 
public class SomeClass
{
    public int x;

    public SomeClass(int a)
    {
        x = a;
    }
}

Quiz:    explanation of the error

Because the constructor NewClass() does not contain any super(...) calls, Java compiler will insert super( ):

public class NewClass
       extends SomeClass
{
    NewClass()
    {
       super( )  
    }
} 
public class SomeClass
{
    public int x;

    public SomeClass(int a)
    {
        x = a;
    }
}

However:   there is no matching constructor ( SomeClass( )) defined in the superclass SomeClass !!! ---> Error

Consequence of the constructor invocation rule in Java

  • Constructor invocation rule in Java:

    • If a class B inherits from a class A, then every constructor in class B must invoke some constructor in class A

    Graphically:

Consequence of the constructor invocation rule in Java

  • Consequence of the Java's constructor invocation rule:

    • If another class C inherits from the class B, then every constructor in class C must invoke some constructor in class B and in class A

    Graphically: this phenomenon is called constructor chaining

Experiment to show    constructor chaining
public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }

    public void method1( )
    {
        System.out.println("I am SomeClass.method1(). x = " + x);
    }
    
    public void method2( )
    {
        System.out.println("I am SomeClass.method2(). x = " + x);
    }
}

public class NewClass extends SomeClass { NewClass() { } }
public class NewerClass extends NewClass { NewerClass() { } }
public class myProg { public static void main(String[] args) { NewClass b = new NewerClass(); // Trace this } }

Summary

  • A subclass inherits all normal members ( including the private members) from its superclass

  • Methods in the subclass cannot access the private inherited members directly

  • A subclass object contains (all members in) a superclass object

  • A subclass do not inherit any constructors from its superclass

  • Because a subclass object contains (all members in) a superclass object, every constructor in the subclass must invoke a constructor in the superclass