Final Exam on 07/28. 

Hopefully make ups (if necessary) available on 07/29. 

Next exam: 07/14 (covers modeling, including in stages).

So far we did modeling by composition (has-a): 

  (a) a Point has an x and a y

  (b) a Line has two Points inside 

  (c) a Triangle has both Points and Lines insides 

When we describe the class extension mechanism we introduce

  * the is-a relationship 

By the end of this week these terms should make sense: 

  - inheritance
  - polymorphism 
  - dynamic method lookup
  - shadowing of variables
  - constructor chaining
  - interfaces
  - abstract classes 

These are almost all consequences of the class extension mechanism. 

Focus on chapters 4, 5, 3. I will provide summmaries soon. 

// This is Screen.java keep it in the same folder with Rectangles.java 

import java.awt.Graphics;
import javax.swing.JComponent; 

public class Screen extends JComponent {
  Rectangle a, b;
  public Screen() {
    this.a = new Rectangle(100, 100, 100,  50); 
    this.b = new Rectangle(120,  75,  30, 120); 
    if (a.overlaps(b)) {
      System.out.println("The two rectangles overlap!");  
    } else {
      System.out.println("The two rectangles do not overlap!");  
    }
  } 
  public void paintComponent(Graphics g) {
    this.a.draw(g); 
    this.b.draw(g); 
  }
}

Now we can start. 

Lab 07 and Homework 05. 

Chris Kayl Levi Seth Jiebo Jerry Jared Alex S Vanessa Mike He He 
Arif Jiahao Kaqin Yinan Arthi Andrew Sunghyun Alex Q Zhiwei Rajeev 
Rerajitha Siddartha Xinyu David Kevin S Brandon P Haoyang Xing 
Chenrui Danny G Matt L

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

Here's how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Horse
Horse@740a7a5
Howdy.
> run Horse
Horse@4ec8a42d
Howdy.
> java Horse
Horse@20c82586
Howdy.
> Horse b = new Horse();
> b
Horse@4f441331
> b.talk()
Howdy.


http://www.entertainmentearth.com/images/AUTOIMAGES/DG7572lg.jpg

public class Unicorn extends Horse {
   
}

With this we can have the following experiments: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse()
> a
Horse@593cc842
> Unicorn b = new Unicorn()
> b
Unicorn@70bbbb6f
> a.talk()
Howdy.
> b.talk() // inheritance
Howdy.
> Horse c = new Unicorn() // polymorphism 
> c
Unicorn@7e9f5462


Modify Unicorn to have a feature: 

public class Unicorn extends Horse {
  public void sing() {
    System.out.println("Hubba hubba!");  
  }
}

We can model inheritance via the set union of features:

  Horse = { talk() } 

  Unicorn = { sing() } U Horse 

Alex S   Only Gosh knows. 
Matt L   Yes 
Yinan    No 
Zhiwei   No 
Mike C   Yes 
Haoyang  No 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn(); 
> Unicorn b = new Horse(); 
Static Error: Bad types in assignment: from Horse to Unicorn
> a
Unicorn@5cd8c8cb
> a.talk()
Howdy.
> a.sing()
Static Error: No method in Horse has name 'sing'
> ((Unicorn)a).sing()
Hubba hubba!


Let's make one more change to Unicorn: 

public class Unicorn extends Horse {
  public void sing() {
    System.out.println("Hubba hubba!");  
  }
  public void talk() {
    System.out.println("Bonjour.");  
  }
}

Here are the experiments now: 

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


How can I still make the Unicorn remember the English it knew. 

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

Now the experiments look like this: 

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


So today we demonstrated: 

  (a) the class extension 

Arif asked if this is possible: 

class A {
  void fun() { System.out.println("I am fun."); }
}

class B extends A {
  void gvo() { System.out.println("My name is gvo."); }
}

class C extends B {
  void hwp() { System.out.println( "I'm hwp." ); }
}

And he wanted to test this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> A a = new A()
> a.fun()
I am fun.
> B b = new B()
> b.fun()
I am fun.
> C c = new C()
> b.gvo()
My name is gvo.
> c.gvo()
My name is gvo.
> c.fun()
I am fun.
> c.hwp()
I'm hwp.


The answer is yes. 

A class can only extend another class. 

But a class can be extended by any number of other classes. 

I will post another example soon. 

Also, here's your Homework 06:

http://www.cs.indiana.edu/classes/h212-dger/spr2016/hw04.html

I'll rewrite it for tomorrow. 

--