We looked at the class extension mechanism.
class Horse {
void think() {
System.out.println("I am thinking...");
}
void talk() {
System.out.println("Howdy");
}
}
class Unicorn extends Horse {
void talk() {
System.out.println("Bonjour");
}
void dream() {
System.out.println("I am dreaming...");
}
}
Class extension mechanism is really based on the set union
of features. But when the new feature we add is of the same
name as the one we were inheriting the new rule of dynamic
method lookup needs to be visited.
class Example {
public static void main(String[] args) {
Horse a;
a = new Horse();
a.think();
a.talk();
// a.dream(); // impossible no justification
// no problem with this
Unicorn b;
b = new Unicorn();
b.think(); // it works, by inheritance
b.talk(); // it's going to print the French greeting
}
}
So we run this and in a few minutes everything becomes:
class Example {
public static void main(String[] args) {
Horse a;
a = new Horse();
a.think();
a.talk();
// a.dream(); // impossible no justification
// no problem with this
Unicorn b;
b = new Unicorn();
b.think(); // it works, by inheritance
b.talk(); // it's going to print the French greeting
b.dream(); // sure
Horse c;
c = new Unicorn(); // all unicorns are horses!
c.talk(); // because Horse has talk defined in it
// c.talk() however prints "Bonjour" because of dynamic method lookup
// 1. because c is of type Horse I can ask for c.talk()
// 2. so the object that c points to gets the message: talk()
// 3. but it's a Unicorn, so it runs its own talk().
// Question: can you make a Unicorn say "Howdy"?
// c.dream(); // not possible because Horses can't dream
c.think();
Unicorn d;
// d = new Horse(); // not all horses are unicorns!
}
}
class Horse {
void think() {
System.out.println("I am thinking...");
}
void talk() {
System.out.println("Howdy");
}
}
class Unicorn extends Horse {
void talk() {
System.out.println("Bonjour");
}
void dream() {
System.out.println("I am dreaming...");
}
}
So this is the end of the review:
class Example {
public static void main(String[] args) {
Horse a;
a = new Horse();
a.think();
a.talk();
// a.dream(); // impossible no justification
// no problem with this
Unicorn b;
b = new Unicorn();
b.think(); // it works, by inheritance
b.talk(); // it's going to print the French greeting
b.dream(); // sure
Horse c;
c = new Unicorn(); // all unicorns are horses!
c.talk(); // because Horse has talk defined in it
// c.talk() however prints "Bonjour" because of dynamic method lookup
// 1. because c is of type Horse I can ask for c.talk()
// 2. so the object that c points to gets the message: talk()
// 3. but it's a Unicorn, so it runs its own talk().
// Question: can you make a Unicorn say "Howdy"?
// c.dream(); // not possible because Horses can't dream
c.think();
Unicorn d;
// d = new Horse(); // not all horses are unicorns!
// from the outside you can't make a Unicorn say "Howdy"
}
}
class Horse {
void think() {
System.out.println("I am thinking...");
}
void talk() {
System.out.println("Howdy");
}
}
class Unicorn extends Horse {
void talk() {
System.out.print("Fine... ");
super.talk();
System.out.println("Bonjour");
}
void dream() {
System.out.println("I am dreaming...");
}
}
Now where is this happening in Java?
/*
*/
import java.applet.*;
import java.awt.*;
public class Tuesday extends Applet {
int count;
public void paint(Graphics g) {
System.out.println("I am painting " + this.count);
this.count += 1;
}
}
This is a simple example of all of what we did above.
Here's a start for more interaction:
/*
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tuesday extends Applet {
int count;
public void init() {
this.addMouseMotionListener(new Referee());
}
public void paint(Graphics g) {
System.out.println("I am painting " + this.count);
this.count += 1;
}
}
class Referee implements MouseMotionListener {
public void mouseMoved(MouseEvent e) {
System.out.println("The mouse is being moved.");
}
public void mouseDragged(MouseEvent e) {
System.out.println("The mouse is being dragged.");
}
}
I make one small simplifying change:
/*
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tuesday extends Applet implements MouseMotionListener {
int count;
public void init() {
this.addMouseMotionListener(this);
}
public void paint(Graphics g) {
System.out.println("I am painting " + this.count);
this.count += 1;
}
public void mouseMoved(MouseEvent e) {
System.out.println("The mouse is being moved.");
}
public void mouseDragged(MouseEvent e) {
System.out.println("The mouse is being dragged.");
}
}
We know where the mouse is:
/*
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tuesday extends Applet implements MouseMotionListener {
int count;
public void init() {
this.addMouseMotionListener(this);
}
public void paint(Graphics g) {
System.out.println("I am painting " + this.count);
this.count += 1;
}
public void mouseMoved(MouseEvent e) {
System.out.println("Mouse moved at: (" + e.getX() + ", " + e.getY() +")");
}
public void mouseDragged(MouseEvent e) {
System.out.println("The mouse is being dragged.");
}
}
But we'll finish next time.
/*
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tuesday extends Applet implements MouseMotionListener {
int count;
public void init() {
this.addMouseMotionListener(this);
}
public void paint(Graphics g) {
System.out.println("I am painting " + this.count);
this.count += 1;
}
public void mouseMoved(MouseEvent e) {
System.out.println("Mouse moved at: (" + e.getX() + ", " + e.getY() +")");
}
public void mouseDragged(MouseEvent e) {
System.out.println("The mouse is being dragged.");
}
}