Attendance:

Team 05: Ruifeng, Elissa, Hassan, Jacob, 

Team 04: Timothy, Charutha, Kaitlyn, Yubang, Yaodong

Team 03: Jing, Viena, Esteban, Eduardo, Topher, Risheng, Ziwei 

Team 02: Nanjie, Seth, Marc, Ross, Mike 

Team 01: Yutong, Mange, Young Ho Yoo, Sean, Madison, Miguel

Team 08: Sam, Tristen, Jason, Baojie, Caleb, Adel, Shengnan 

Team 07: Ben, Tyler, Thomas, Lin, Cynthia, Vinoth, Dian 

Team 06: Hyngman, Jackson, Cory, Manasse, Ryan, Michael John 

Questions: 

1. What is a Horse?

A very complicated animal with a lot of features. 

2. What is a Unicorn? 

https://d1jqecz1iy566e.cloudfront.net/large/md2191.jpg

A Unicorn is a Horse with a horn. 

3. Why do they put bells on cows? 

Their horns don't work. 

Java programs are made out of classes. 

Classes are containers of static members and blueprints. 

We instantiate classes to create objects.

Classes are used for modeling. 

Let's model a Horse. 

public class Horse {
  public void talk() {
    System.out.println("Howdy."); 
  }
}

It works like this: 

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Horse a = new Horse();
> a
Horse@110ec9b
> a.talk()
Howdy.
> Horse b = new Horse()
> b
Horse@decfc5
> b.talk()
Howdy.


So we can say Horse = { talk() }

Unicorn = Horse U { sing() } 

So the class extension mechanism helps you model in stages. 

It can be represented easily by set union of features. 

Here's how it is in Java: 

public class Unicorn extends Horse {

}

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Horse a = new Horse()
> a
Horse@1a63c4c
> a.talk()
Howdy.
> Unicorn b = new Unicorn()
> b
Unicorn@530da5
> b.talk() // by inheritance
Howdy.
> Horse c = new Unicorn()
> // the reason is: polymorphism
> Unicorn d = new Horse()
Static Error: Bad types in assignment: from Horse to Unicorn


Now let's look at dynamic method lookup:

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Horse a = new Unicorn()
> a
Unicorn@a001b5
> a.talk()
Howdy.
> a.sing()
Static Error: No method in Horse has name 'sing'
> ((Unicorn)a).sing()
That's me in the unicorner...


That's not yet dynamic method lookup. 

My problem is set union. 

{ a, c } U { d } = { a, c, d }

{ a, c } U { d, c } = ?

Horse = { talk() }

Unicorn = Horse U { sing(), talk() } 

So let's see what happens:

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Horse a = new Horse()
> a.talk()
Howdy.
> Unicorn b = new Unicorn()
> b.sing()
That's me in the unicorner...
> b.talk()
Bonjour.
> Horse c = new Unicorn()
> c.talk()
Bonjour.


Dynamic method lookup is also called over-riding.

Constructor chaining:

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Horse a = new Horse()
> a.talk()
Howdy, I am null
> a.name = "Leslie"
"Leslie"
> a.talk()
Howdy, I am Leslie
> Unicorn b = new Unicorn()
> b.name
null
> b.talk()
Bonjour.


Do I have a constructor for Horse as the class is right now? 

public class Horse {
  public Horse() {
    
  }
  public Horse(String name) {
    this.name = name;  
  }
  String name; 
  public void talk() {
    System.out.println("Howdy, I am " + this.name); 
  }
}

public class Unicorn extends Horse {
  public void sing() {
    System.out.println("That's me in the unicorner...");  
  }
  public void talk() {
    System.out.println("Bonjour.");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Horse a = new Horse("James")
> a
Horse@1a63c4c
> a.talk()
Howdy, I am James


Let's get rid of the constructor in the Horse class that creates anonymous Horses.

When a Horse object is created a Horse object is created and initialized by the
corresponding constructor. When a subclass is instantiated the fact that the object
we need has in fact a model that has been described in stages will become obvious. If
I want to create a Unicorn I will need to have a Horse first, then I can add all the
features that make the Horse object a special one (of type Unicorn).

How does Java ensure this?

It will explicitly call a constructor of the superclass unless you do that. 

public class Unicorn extends Horse {
  Unicorn(String name) {
    super(name);
    // now add the horn etc. ...
  }
  public void sing() {
    System.out.println("That's me in the unicorner...");  
  }
  public void talk() {
    System.out.println("Bonjour.");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Unicorn a = new Unicorn("sKDLJFhxds");
> a
Unicorn@29d29f
> a.talk()
Bonjour.
> a.name
"Misty"


1. How does this explain what was happening when we asked Adel? 

2. How can make a Unicorn speak English again? 

public class Unicorn extends Horse {
  Unicorn(String name) {
    super(name);
    // now add the horn etc. ...
  }
  public void sing() {
    System.out.println("That's me in the unicorner...");  
  }
  public void talk() {
    super.talk(); 
    System.out.println("Bonjour.");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Unicorn a = new Unicorn("Laura")
> a
Unicorn@18d63dc
> a.talk()
Howdy, I am Laura
Bonjour.


The first question:

  Unicorn() {
    super(); 
  }

--



https://d1jqecz1iy566e.cloudfront.net/large/md2191.jpg