Notes for Fri July 17, 2009. Experiment 1: class Person { void talk() { System.out.println("Howdy."); } } class One { public static void main(String[] args) { Person a = new Person(); a.talk(); } } I am defining a model for a person. A person can talk, saying: Howdy. Experiment 2: class Person { void talk() { System.out.println("Howdy."); } } class One { public static void main(String[] args) { Person a = new Person(); a.talk(); Student b = new Student(); b.talk(); } } class Student extends Person { } A student is everything a person is (and I can add to that, but I am not here). Experiment 3: class Person { void talk() { System.out.println("Howdy."); } } class One { public static void main(String[] args) { Person a = new Person(); a.talk(); Student b = new Student(); b.talk(); System.out.println( b.add(3, 5) - 2 ); } } class Student extends Person { int add(int n, int m) { return n + m; } } The new feature is add. Experiment 4: class Person { void talk() { System.out.println("Howdy."); } } class Student extends Person { int add(int n, int m) { return n + m; } } class ExchangeStudent extends Student { void talk() { System.out.println("Bonjour"); } } class One { public static void main(String[] args) { Person a = new Person(); a.talk(); Student b = new Student(); b.talk(); System.out.println( b.add(3, 5) - 2 ); ExchangeStudent c = new ExchangeStudent(); c.talk(); } } We can have more than two stages. Experiment 5: class Person { void talk() { System.out.println("Howdy."); } } class Student extends Person { int add(int n, int m) { return n + m; } void talk() { System.out.println("Bonjour"); } } class One { public static void main(String[] args) { Person a = new Person(); a.talk(); // this prints Howdy Student b = new Student(); b.talk(); // this prints Bonjour but why?... Person c = new Student(); // polymorphism c.talk(); // prints Bonjour // dynamic method lookup // System.out.println(c.add(3, 1)); // Student d = new Person(); // not allowed } } Experiment 6: /* */ import java.applet.*; import java.awt.*; public class One extends Applet { int i; public void paint(Graphics g) { i += 1; System.out.println("I am painting..." + i); } } Experiment 7: class Person { void talk() { System.out.println("Howdy."); } } class Student extends Person { int add(int n, int m) { return n + m; } void talk() { super.talk(); System.out.println("Bonjour"); } } class One { public static void main(String[] args) { Person c = new Student(); c.talk(); } } Experiment 8: /* */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class One extends Applet { public void init() { Two referee = new Two(); this.addMouseMotionListener(referee); } public void paint(Graphics g) { } } class Two implements MouseMotionListener { public void mouseMoved(MouseEvent e) { System.out.println("Mouse moved at (" + e.getX() + ", " + e.getY() + ")"); } public void mouseDragged(MouseEvent e) { System.out.println("Mouse dragged..."); } } This further emphasized that in Java typically we override/provide only pieces of an otherwise much bigger mechanism. Experiment 9 (just with Nathan): /* */ import java.applet.*; import java.awt.*; public class One extends Applet { public void paint(Graphics g) { g.drawString("I am here", 50, 50); } } Experiment 10 (also with Nathan): /* */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class One extends Applet { public void init() { Two referee = new Two(this); this.addMouseMotionListener(referee); } public void paint(Graphics g) { } } class Two implements MouseMotionListener { One applet; int px, py; Two(One applet) { this.applet = applet; px = -1; py = -1; } public void mouseMoved(MouseEvent e) { System.out.println("Mouse moved at (" + e.getX() + ", " + e.getY() + ")"); this.px = -1; this.py = -1; } public void mouseDragged(MouseEvent e) { System.out.println("Mouse dragged..."); Graphics g = this.applet.getGraphics(); int x = e.getX(); int y = e.getY(); if (this.px < 0) { } else { g.drawLine(px, py, x, y); } this.px = x; this.py = y; } } Over the weekend I am going to work on your assignments and will write to you individually.