Learn how to model.

public class Horse {
  public Horse() {

  }
  public static void main(String[] args) {
    Horse a = new Horse(); 
    System.out.println( a ); 
  }
}

We write some more and here is the final version:

public class Horse {
  public Horse() {

  }
  public void talk() {
    System.out.println("Howdy, I am " + this);  
  }
  public static void main(String[] args) {
    Horse a = new Horse();
    System.out.println( a );
    a.talk(); 
    Horse b = new Horse(); 
    b.talk(); 
  }
}

What is a Unicorn? 

public class Unicorn extends Horse {
   
}


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse()
> a
Horse@7ba5f728
> a.talk()
Howdy, I am Horse@7ba5f728
> Unicorn b = new Unicorn()
> b
Unicorn@633e836a
> b.talk() // inheritance
Howdy, I am Unicorn@633e836a
> Horse c = new Unicorn() // polymorphism 
> c.talk()
Howdy, I am Unicorn@fc73c67


Now we make a change:

public class Unicorn extends Horse {
  public void sing() {
    System.out.println("La la la..."); 
  }
}

Here's how this works now: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Unicorn a = new Unicorn()
> a
Unicorn@55377d0b
> a.talk()
Howdy, I am Unicorn@55377d0b
> a.sing()
La la la...
> Horse a = new Unicorn()
> a.talk()
Howdy, I am Unicorn@3bbfefef
> a.sing()
Static Error: No method in Horse has name 'sing'
> ((Unicorn)a).sing()
La la la...


I had to cast to get full functionality out of a. 

public class Unicorn extends Horse {
  public void sing() {
    System.out.println("La la la..."); 
  }
  public void talk() {
    System.out.println("Bonjour."); 
  }
}

How does this work?

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse()
> a.talk()
Howdy, I am Horse@22a7bec7
> a.sing()
Static Error: No method in Horse has name 'sing'
> Unicorn b = new Unicorn()
> b.sing()
La la la...
> b.talk()
Bonjour.
> Horse c = new Unicorn()
> c
Unicorn@6b697231
> c.talk()
Bonjour.
>

How do I get a Unicorn to speak English.

public class Unicorn extends Horse {
  public void sing() {
    System.out.println("La la la..."); 
  }
  public void talk() {
    super.talk();
    System.out.println("Bonjour."); 
  }
}

How does this work? 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn()

> a.talk()
Howdy, I am Unicorn@572791f0
Bonjour.


Let's put names inside the Horses. 

public class Horse {
  String name;
  public Horse(String name) {
    this.name = name; 
  }
  public void talk() {
    System.out.println("Howdy, I am " + this.name);  
  }
  public static void main(String[] args) {
    Horse a = new Horse("Leslie");
    System.out.println( a );
    a.talk(); 
    Horse b = new Horse("Alex"); 
    b.talk(); 
  }
}

Here's how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Horse
Horse@1155df11
Howdy, I am Leslie
Howdy, I am Alex


Does the Unicorn still work? 

public class Unicorn extends Horse {
  public Unicorn() {
    super();  
  }
  public void sing() {
    System.out.println("La la la..."); 
  }
  public void talk() {
    super.talk();
    System.out.println("Bonjour."); 
  }
}

No, because constructor chaining is now broken. 

public class Horse {
  String name;
  public Horse(String name) {
    this.name = name; 
  }
  public void talk() {
    System.out.println("Howdy, I am " + this.name);  
  }
  public static void main(String[] args) {
    Horse a = new Horse("Leslie");
    System.out.println( a );
    a.talk(); 
    Horse b = new Horse("Alex"); 
    b.talk(); 
    Horse c = new Unicorn("Ryan"); 
    c.talk(); 
    ((Unicorn)c).sing();
  }
}

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

Compile and run Horse and you get: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Horse
Horse@4a408a84
Howdy, I am Leslie
Howdy, I am Alex
Howdy, I am Ryan
Bonjour.
La la la...


Minute paper: arrays and array lists contain elements of the same type. 

Can you define Point, Line, Circle, Triangle and Rectangle? 

Can you create a Circle, a Triangle and a Rectangle and put them all in the same array? 

class Point {
  
}

class Line {
  
}

class Circle {
  
}

class Triangle {
  
}

class Rectangle {
  
}

public class Exercise {
  public static void main(String[] args) {
    Object[] a = new Object[3]; 
    Circle c = new Circle(); 
    Rectangle r = new Rectangle();
    Triangle t = new Triangle(); 
    a[0] = c; 
    a[1] = r;
    a[2] = t; 
    for ( Object thatThing : a)
      System.out.println( thatThing ); 
  }
}

It runs like this:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Exercise
Circle@3a3260df
Rectangle@3816656a
Triangle@1e7d4d4b


--

   pgouldin lab 06 attendance