8:13 AM 3/9/2015

1. What is the class extension mechanism? 

2. Why do we care? 

Answers:

1. Allows you to model in stages. 

2. It generates

  2.1 Inheritance

  2.2 Polymorphism

  2.3 Dynamic Method Lookup

  2.4 Constructor Chaining

Can we describe those? 

2.1 Here's an example:

class A /* extends Object */ {
  void fun() {
    System.out.println( "Howdy." );  
  }
  public static void main(String[] args) {
    A a = new A(); 
    System.out.println( a.toString() ); 
  }
}

I inherit toString() from Object:

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()

http://www.charlottesvilleanimalcontrol.com/wp-content/uploads/2011/10/Southern-Flying-Squirrel-Photo-Credit-Joe-McDonald2.jpg

http://zestytravel.com/wp-content/uploads/2012/04/Hand-Gliding-France-2.jpg

2.2 Polymorphism

class A /* extends Object */ {
  void fun() {
    System.out.println( "Howdy." );  
  }
  public static void main(String[] args) {
    A a = new A(); 
    System.out.println( a.toString() ); // inheritance
    Object b = a; // polymorphism
    ((A)b).fun();
  }
}

2.3 Dynamic Method Lookup (Overriding of Methods) 

class A /* extends Object */ {
  void fun() {
    System.out.println( "Howdy." );  
  }
  public static void main(String[] args) {
    Object a = new A(); 
    System.out.println( a.toString() ); // dynamic method lookup 
  }
  public String toString() {
    return "I am an A.";  
  }
}

2.4 Constructor Chaining 

What do we know about constructors? 

class A {
  public static void main(String[] args) {
    A a = new A();  
  }
}

If you don't specify any, you get a default no-arg constructor. 

You lose the default when you start specifying your own constructors. 

class A {
  A(String a) {
    
  }
  public static void main(String[] args) {
    A a = new A();  
  }
}

This doesn't compile so we must lost something. 

But we can put it back: 

class A {
  A(String a) {
    
  }
  A() {
    
  }
  public static void main(String[] args) {
    A a = new A();  
  }
}

So now we're ready:

What is constructor chaining? 

Java guarantees that when an object of some class is created one of the 
constructors from that class is invoked.  Moreover when an object of a 
subclass is created the constructor from the superclass is invoked first. 

Constructors have rules. We've seen some. 

class B {
  int a; 
  B() {
    // ... [1] 
    this(3);  
    System.out.println( "I am back from the other constructor..." ); // [2] 
  }
  B(int a) {
    System.out.println("I have received " + a);    
    this.a = a; 
  }
  public static void main(String[] args) {
    B a = new B(6); // prints I have received 6
    a = new B(2); // prints I have received 2
    a = new B(); // prints I have received 3 then I am back ...
    a = new B(3); // prints I have received 3 
    
  }
}

This is not constructor chaining. 

But it is a way of sequencing constructor invocations. 

It's also demonstrating a strange rule: this(...) is first always. 

What is constructor chaining? 

class A {
  A() {
    System.out.println( "The A part is being worked on. "); 
  }
  public static void main(String[] args) {
    A a = new A();  
  }
}

class C extends A {
  C() {
    System.out.println(" The C part is being worked on. ");  
  }
}

class Example {
  public static void main(String[] args) {
    C c = new C();  
  }
}

This is constructor chaining. 

What else is left to say? 

class A {

}

class C extends A {

}

class Example {
  public static void main(String[] args) {
    C c = new C();  
  }
}

The code above is equivalent to the one below: 

class A {
  A() {

  }
}

class C extends A {
  C() {
   
  }
}

class Example {
  public static void main(String[] args) {
    C c = new C();  
  }
}

And the code above is equivalent to the one below:

class A {
  A() {

  }
}

class C extends A {
  C() {
    super();    
  }
}

class Example {
  public static void main(String[] args) {
    C c = new C();  
  }
}

So how do we state this? 

Constructor Chaining

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.

--