1. Here's Horse below. (We are modeling Mr. Ed, at least in the beginning.) 

 2. Unicorn is an extension of Horse: a horse with a horn. 

 3. It's being able to model in stages: first Horse, then add extra features for Unicorn. 

 4. Inheritance is what allows Unicorn objects to use the features described in class Horse. 

 5. Polymorphism is the ability of Unicorn objects to be referred (accessed) through both variables of type Horse and Unicorn. 

Now a good question would be: what is casting? 

 6. I overrode talk from Horse in Unicorn. 

 7. Redefining an inherited method is called overriding. 

 8. Defining two or more methods or constructors with the same name in a class is called overloading (that name). 

 9. Dynamic method lookup is how overriding manifests itself from the outside of the object. 

10. The this keyword is a form of self-reference. 

It's how an instance method can identify its host (the object/instance in which it resides). 

11. A Unicorn object can speak English but it has to want it. You can't make it. 

12. The super keyword is this cast to the type of the superclass. 

13. Yes, any class has a constructor even if you don't see it. 

The rule is: if you don't specify a constructor you will be given the default no-arg constructor. 

That's a constructor that doesn't take any argument and does nothing. 

Here's everything up to this point: 

class Horse {
  public void talk() {
    System.out.println("I am a Horse."); 
  }
  public void talk(String language) { // overloading 
    if (language.equals("French")) 
      System.out.println("Bonjour."); 
    else if (language.equals("Russian")) 
      System.out.println("Privyet"); 
    else 
      System.out.println("I do not speak " + language); 
  }
}

class Unicorn extends Horse {
  public void sing() {
    System.out.println("That's me in the (Uni)corner..."); 
  } 
  public void talk() { // overriding 
    System.out.println("Je suis une licorne."); 
  } 
}

class LetMeShowYou {
  public static void main(String[] args) {
    Unicorn a = new Unicorn(); 
    System.out.println( a ); 
    a.sing(); // works, by definition
    a.talk(); // works, by inheritance
    Horse b;
    b = new Horse(); 
    b.talk(); // works, by definition
    // b.sing(); // no, feature not available 
    Horse c = new Unicorn(); // polymorphism 
    System.out.println( c ); 
    c.talk(); 
    ((Unicorn)c).sing(); // works by casting 
    Horse d = new Horse(); 
    d.talk(); 
    d.talk("French"); 
    d.talk("Russian"); 
    d.talk("Japanese"); 
    Unicorn e = new Unicorn(); 
    e.talk(); // dynamic method lookup 
    Horse f = new Unicorn(); 
    f.talk(); // dynamic method lookup 
  } 
}


14. Now Horse objects can carry a name, see below. 

15. I have overloaded the constructor in Horse and one calls the other with this(...)

16. An invocation of that kind can only be the first line of a constructor. 

So swapping the lines (printing with the invocation) will prevent the code from compiling. 

17. Constructor chaining is what happens when an object of a subclass is created. 

18. The rules are (as stated in http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/TwentyEight.html): 

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.

What this all means is that constructor calls are "chained" -- any time an object is created, a sequence of constructor 
methods are invoked, from subclass to superclass on up to Object at the root of the class hierarchy. Because a superclass 
constructor is always invoked as the first statement of its subclass constructor, the body of the Object constructor always 
runs first, followed by the body of its subclass, and on down the class hierarchy to the class that is being instantiated. 

So when you ask for a Unicorn you can be sure that the Horse part gets created first (as it should)!

class Horse {
  String name; 
  Horse() { 
    this("Anonymous.");
    System.out.println("I can't take it any more.");
  } 
  Horse(String name) {
    this.name = name; 
  }
  void talk() {
    System.out.println("Howdy, I am " + this.name); 
  } 


class Unicorn extends Horse {
  Unicorn(String name) {
    super(name); // constructor chaining
  }
  void talk() {
    super.talk(); 
    System.out.println("Bonjour."); 
  }
  void sing() {
    System.out.println("La, la..."); 
  }
}

class Whatever {
  public static void main(String[] args) {
    Horse a = new Horse("Larry");
    a.talk(); 
    Unicorn b = new Unicorn("Francois"); 
    b.talk(); 
  }
}

19. Of course, plenty. 

Here's an example of extending a Horse named JFrame. 

20. This one is from javax.swing.JFrame

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

class Unicorn extends javax.swing.JFrame {
  Unicorn() {
    this.setSize(400, 300); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(3); 
  }
}

See you in lab!

--