Constructor Chaining

Case-Study: Drawing with the Mouse

public class Horse {
  String name;
  public Horse(String name) {
    this.name = name;
  }
  public void talk() {
    System.out.println( "Hello, my name is " + this.name );  
  }
  public static void main(String[] args) {
    Horse a = new Horse("Adrian");  
    a.talk(); 
  }
}

The class extension mechanism: we can model in stages. 

The class extension mechanism is as simple as the set union of features. 

Horse = { talk }

Unicorn = Horse U { sing } 

class Unicorn extends Horse leads to that 

-- inheritance 

-- polymorphism 

-- dynamic method lookup (overriding) 

-- shadowing of variables 

Let's do this again: 

public class Horse {
  String name;
//  public Horse(String name) {
//    this.name = name;
//  }
  public void talk() {
    System.out.println( "Hello, my name is " + this.name );  
  }
  public static void main(String[] args) {
    Horse a = new Horse(); // "Adrian");  
    a.talk(); 
  }
}

public class Unicorn extends Horse {
  
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn();
> a.talk();
Hello, my name is null
> a
Unicorn@352e855d


Now if I modify Unicorn:

public class Unicorn extends Horse {
  public void talk() {
    System.out.println( " Bonjour, je m'appele " + this.name );  
  }
}

I have: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn(); // polymorphism
> a.talk(); // dynamic method lookup
 Bonjour, je m'appele null


Research problem: google Java constructor chaining. What is it?

http://www.cs.indiana.edu/classes/a201-dger/sum2004/notes/Eleven.html

http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/TwentyEight.html

The rules of constructor chaining (from Java by Example by David Flanagan): 

When you define a class, Java guarantees that the class's constructor method is 
called whenever an instance of that class is created. It also guarantees that the 
constructor is called when an instance of any subclass is created. In order to 
guarantee this second point, Java must ensure that every constructor method calls 
its superclass constructor method. If the first statement in a constructor is not an 
explicit call to a constructor of the superclass with the super keyword, then Java 
implicitly inserts the call super() -- that is, it calls the superclass constructor 
with no arguments. If the superclass does not have a constructor that takes no 
arguments, this causes a compilation error.

There is one exception to the rule that Java invokes super() implicitly if you do 
not do so explicitly. If the first line of a constructor, C1, uses the this() syntax 
to invoke another constructor, C2, of the class, Java relies on C2 to invoke the 
superclass constructor, and does not insert a call to super() into C1. Of course, if 
C2 itself uses this() to invoke a third constructor, C2 does not call super() 
either, but somewhere along the chain, a constructor either explicitly or implicitly 
invokes the superclass constructor, which is what is required.

One way to eliminate the error is to do this: 

public class Unicorn extends Horse {
  public Unicorn() {
    super(); 
    
  }
  public void talk() {
    System.out.println( " Bonjour, je m'appele " + this.name );  
  }
}

So I can avoid a compilation error like this:

public class Horse {
  String name;
  Horse() {
    
  }
  public Horse(String name) {
    this.name = name;
  }
  public void talk() {
    System.out.println( "Hello, my name is " + this.name );  
  }
  public static void main(String[] args) {
    Horse a = new Horse("Adrian");  
    a.talk(); 
  }
}

But that's not what I wanted. 

public class Unicorn extends Horse {
  public Unicorn(String name) {
    super(name); 
    
  }
  public void talk() {
    System.out.println( " Bonjour, je m'appele " + this.name );  
  }
}

This works as follows: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn("Misty");
> a
Unicorn@26d96dee
> a.talk()
 Bonjour, je m'appele Misty


At this point we have reviewed most of the phenomena we wanted to review. 

Exercises (optional) in lab. 

Task: draw using the mouse on a JFrame. 

import javax.swing.*; 
import java.awt.*; 

public class Game extends JFrame {
  public Game() {
    // super();  
    this.setVisible( true ); 
    this.setSize( 500, 500 ); 
    Container c = this.getContentPane(); 
    c.add( new Screen() ); 
  }
  public static void main(String[] args) {
    JFrame f = new Game(); // polymorphism  
  }
}

import javax.swing.*;
import java.awt.*; 

public class Screen extends JComponent {
  public Screen() { // constructor chaining
    super(); 
    
  }
  public void paintComponent(Graphics g) {
    g.drawString("This is great.", 50, 50);  
  }
}

Run it, look at the text. 

Set up the next goal: mouse coordinates shown as string at the location of the mouse. 

--