Relationship between a subclass and its superclass

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

 

Relationship between a subclass and its superclass

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

But instead:   a constructor in the subclass must invoke some constructor in the superclass -- discussed next

Relationship between a subclass and its superclass

Notice that a subclass object always contains a superclass object:

Recall:   objects are initialized using a constructor    --    how to makes sure that a subclass object is initialized ?

Relationship between a subclass and its superclass

(3) Rule:   a constructor of the subclass must invoke some constructor of its superclass as the first statement

The syntax super( parameters ) 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 NewClass()
  {
    super(); //Calls: SomeClass()
  }
    
  public NewClass(int a)
  {
    super(a); //Calls: SomeClass(a)
  }
}
  
public class SomeClass
{
  public int x;

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

DEMO: demo/13-inheritance/06-super

DEMO: trace the execution in BlueJ

Relationship between a subclass and its superclass

(4) Compliance rule:   if a constructor in the subclass does NOT invoke any constructor in its superclass:

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

Relationship between a subclass and its superclass

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

I.e.:   if the first statement in a constructor is not super(...), the Java compiler will call the default constructor!

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 NewClass()
  {
    // No super( ) call
    // Java will insert super()
  }
    
  public NewClass(int a)     
  {
    super(a);      
  }
}
  
public class SomeClass
{
  public int x;

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

DEMO: demo/13-inheritance/07-super

DEMO: trace the execution in BlueJ

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

DEMO: demo/13-inheritance/09-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) { NewerClass x = new NewerClass(); // Trace this } }

Summary

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

    • 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

    Constructor chaining:  


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