name/e-mail/03-09-2015 

  1. What is the class extension mechanism? 

  2. Why do we care? 

Answers:

  1. Allows you to model in stages. 
 
     It works sort of like the set union of features.

http://www.cs.indiana.edu/classes/c212/fall2010/notes/inherit002.jpg

  2. It produces: 

    2.1 Inheritance 

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

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

http://www.jacksonmountainhomes.com/blog/wp-content/uploads/2008/07/flying-squirrel.jpg

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

The second one is closer to composition. 

    2.2 Polymorphism

Differences between republicans and democrates vanish during war time. 

Polymorphism is a way of ignoring differences between otherwise distinctly specialized objects. 

class A /* extends java.lang.Object */ {
    
  public static void main(String[] args) {
    A a = new A(); 
    
    Object b = new A(); // polymorphism 
    b = a; // still polymorphism 
  }
}

Here's another example in which A is adding new features: 

class A /* extends java.lang.Object */ {
  void fun() {
    System.out.println("How are you?"); 
  }
  public static void main(String[] args) {
    A a = new A(); 
    a.fun(); // [1]  
    
    Object b = new A(); // polymorphism 
    b = a; // still polymorphism 
    
    (new A()).fun(); // [2] 
    
    System.out.println( b );  
    
    ((A)b).fun();
  }
}

    2.3 Dynamic Method Lookup (Method Overriding)

class A /* extends java.lang.Object */ {
  public String toString() {
    return "I am an A. " + super.toString();  
  }
  public static void main(String[] args) {
    Object a = new A(); 
    System.out.println( a.toString() ); 
    
  }
}

    2.4 Constructor Chaining 

Java has many rules. Some are kinda invisible. Particularly about constructors. 

For example calls to this(...) have to start the constructor. 

class A {
  A() {
    // System.out.println("I am starting the constructor with no arguments.");
    this(3);  
    System.out.println("There.");
  }
  A(int a) {
    System.out.println( a ); 
  }
  public static void main(String[] args) {
    A a = new A(-2);  // prints -2
    A b = new A(3);  // prints 3
    A c = new A(6);  // prints 6
    A d = new A();  // prints 3 followed by There.
  }
}

Here's what constructor chaining is about:

class A {
  A() {
    System.out.println(" An A is being created. "); 
  }
}

class B extends A {
  B() {
    // super(); 
    System.out.println( "A B is being created. "); 
  }
}

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

If you run this you will experience constructor chaining. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Example
 An A is being created. 
A B is being created. 


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.

--