Professional Documents
Culture Documents
CS 1311X
Our Story So Far
The Story of O
Inheritance, Polymorphism and Death
in the Afternoon
X11
13
Our Story So Far
• Object Oriented Programming Features
– Encapsulation
– Reusability
– Adaptability
X11
13
Our Story So Far
• Java Language Syntax
• Class Structure
– Fields
– Constructors
– Methods
• Accessors
• Modifiers
– Special Concepts
• toString
• main
• static
X11
13
Our Story So Far
• All objects are manipulated using references
X11
13
The Story of O
CS Version
X11
13
The Story of O
Object
Oriented
X11
13
The Story of O
• Operator Overloading
int a = 3;
int b = 2;
int c;
String x = "Hello ";
String y = "World!";
String z;
c = a + b;
z = x + y;
X11
13
The Story of O
• Other Overloading
– Constructor
public Student(String name, double
gpa)
public Student(String name)
– Method
public int max(int a, int b)
public int max(int a, int b, int c)
public int max(short a, short b)
X11
13
The Story of O
• Overriding
– Also called redefinition
class A {
int someMethod()
}
class B extends A
int someMethod()
}
X11
13
The Story of O
Everything
is an
Object! X11
13
Inheritance
class Animal
{
String name;
X11
13
Inheritance
class Dog extends Animal
{
int fleas;
X11
13
Inheritance
class Cat extends Animal
{
int hairBalls;
X11
13
Inheritance (Deceptive Diagram)
Animal
Dog Cat
X11
13
Inheritance (True Diagram)
Object
Animal
Dog Cat
X11
13
Truth Be Known
• Class Object exists
• It defines lots of useful methods
– e.g. toString
– see the API
• So what?
X11
13
Motivation
• Repetitive tasks
X11
13
Motivation
• Collections are seldom uniform
X11
13
Recall
float f;
double d ;
int i;
String s;
CokeMachine cm;
f = d; // illegal
d = f; // legal
i = d; // illegal
d = i; // legal
X11
13
Recall
float f;
double d ;
int i;
String s;
CokeMachine cm;
f = (float)d; // legal
d = f; // legal
i = (int)d; // legal
d = i; // legal
X11
13
Recall
float f;
double d ;
int i;
String s;
CokeMachine cm;
s = cm; // illegal
cm = s; // illegal
X11
13
Recall
float f;
double d ;
int i;
String s;
CokeMachine cm;
s = (String)cm; // illegal
cm = (CokeMachine)s; // illegal
X11
13
Inheritance Changes the Rules
Object
Animal
Dog Cat
X11
13
Inheritance Changes the Rules
Object o;
Animal a;
Dog d = new Dog();
Cat c = new Cat();
d = c; // illegal
a = c; // OK, a Cat is an Animal
o = c; // OK, a Cat is an Object
o = a; // OK, an Animal is an Object
a = o; // Illegal, not all Objects are Animals
d = a; // Illegal, not all animals are Dogs
Confusing?
13 X11
The Keyword is Extends
Object creation process
Dog d = new Dog();
1. Create reference d
2. Start creating Dog by entering Dog constructor
and making call to parent.
3. Start creating Animal by entering Animal
constructor and making call to parent.
4. Create Object portion
5. Create Animal portion
6. Create Dog portion
13 X11
Step by Step
Dog d
Ref: Dog
d
X11
13
Step by Step
}
Ref: Dog
d
13 X11
Step by Step
}
Ref: Dog
d public Animal()
{
13 X11
Step by Step
public Object()
{
13 X11
Step by Step
13 X11
Step by Step
13 X11
Step by Step
Object
Ref: Dog Animal
d Dog
X11
13
Step by Step
Object
Ref: Dog Animal "A Dog Object"
d Dog
13 X11
Quizlette
Animal a = new Dog();
Object
Ref: Animal Animal "A Dog Object"
a Dog
13 X11
Quizlette
Object o = new Dog();
Object
Ref: Object Animal "A Dog Object"
o Dog
13 X11
Quizlette
Dog d = new Animal();
Object
Ref: Dog Animal "An Animal Object"
d
ILLEGAL
X11
13
Quizlette
Animal a = new Object();
Object
Ref: Animal "An Object Object"
a
ILLEGAL
13X11
Quizlette
Dog d = new Object();
X11
13
Quizlette
Dog d = new Object();
Object
Ref: Dog "An Object Object"
d
ILLEGAL
13X11
Same Logic
d = o;
Ref: Dog
d
?
Ref: Object
o
o = d;
Ref: Object
OK!
o
?
Ref: Dog
d
X11
13
Don't be confused!!!
• Primitives
double d;
float f;
d = f; // legal...no loss of
information
f = d; // illegal...potential loss of
// information
X11
13
Don't be confused!!!
•References
Object o;
Dog d;
Pearls p;
Swine s;
p = (Pearls)s;
X11
13
What's the Point?
• Java Philosophy: Catch errors at compile time.
X11
13
Dynamic Binding
• The heart of polymorphism
Object
toString()
Animal A Dog
Ref: Animal Object
Dog
a
toString()
13 X11
Trick #3
• Java checks types at compile time when
assigning references (Run time checking is also
performed).
13X11
More Quizlette Fun
Object o = new Dog();
Object
Animal a = new Dog(); toString()
Dog d = new Dog(); Object
Animal
toString()
o.toString(); move()
o.move(); Animal
o Dog move()
Object
o.bark(); move()toString()
a.toString(); Dog
toString()
Animal
move()
a.move(); a bark()
move()
toString()
a.bark(); bark()
Dog
d.toString(); d move()
d.move(); toString()
d.bark(); bark()
13 X11
Back to Collections
• The simple approach
Object
X11
13
Java Collections
• LinkedList
• ArrayList
• Vector
• Stack
• HashSet
• TreeSet
• HashTable
• Plus you'll write lots on your own...
Object
X11
13
Using Generic Collections -- Betterly???
LinkedList zoo = new LinkedList();
Object o = new Dog();
Pig p = new Pig();
zoo.add(o);
zoo.add(new Cat());
zoo.add(p);
while(zoo.size() > 0) {
o = zoo.removeFirst();
o.talk(); // Does this work???
}
X11
13
Using Generic Collections -- Betterly???
LinkedList zoo = new LinkedList();
Object o = new Dog();
Pig p = new Pig();
zoo.add(o);
zoo.add(new Cat());
zoo.add(p);
while(zoo.size() > 0) {
o = zoo.removeFirst();
((???))o.talk(); // Does this work???
}
X11
13
Mystery Partially Solved
Object
Animal
talk()
X11
13
Using Generic Collections -- Correctly
LinkedList zoo = new LinkedList();
Animal a = new Dog();
Object o = new Dog();
Pig p = new Pig();
zoo.add(a);
zoo.add(o);
zoo.add(new Cat());
zoo.add(p);
while(zoo.size() > 0) {
o = zoo.removeFirst();
((Animal))o.talk();
}
X11
13
Software Engineering
• What should the talk method in class Animal do?
13 X11
How many will forget?
X11
13
Solution: Abstract Methods and Classes
class Animal
{
public abstract void talk();
// Note: No body!
}
Sometimes called a deferred
method...
Result: Error!!!
13 X11
Solution: Abstract Methods and Classes
abstract class Animal
{
public abstract void talk();
// Note: No body!
Result: Okay.
X11
13
Java Never Forgets
• Any subclass of class Animal has two choices:
– Be abstract
X11
13
Or
class Dog extends Animal
{
public void talk()
{
System.out.println("Bow wow!);
}
}
X11
13
Just for the Record
abstract class Canine
{
Object
// define other stuff?
}
abstract Animal
abstract talk()
abstract Canine
Cat Pig
talk() talk()
Dog Wolf
talk() talk()
13X11
Using Generic Collections -- Correctly
LinkedList zoo = new LinkedList();
Animal a = new Dog();
Object o = new Dog(); Object
Pig p = new Pig();
zoo.add(a);
abstract Animal
zoo.add(o); abstract talk()
zoo.add(new Cat());
zoo.add(p);
while(zoo.size() > 0) { Dog Cat Pig
o = zoo.removeFirst(); talk() talk() talk()
((Animal))o.talk();
}
13X11
Design Guidelines
• Class hierarchy design is important
– Move common methods up the tree
– Use abstract methods appropriately
– May be iterative process
• Container classes should generally hold Objects
• Learn and understand the Java rules concerning
– Type checking
– Reference checking
– Dynamic binding
• You'll be glad you did!
13 X11
Back to the Problem at Hand
X11
13
Coke Machines Available
• CokeMachine
• Standard Model
• Requires Exact Change
• Low Cost
X11
13
Coke Machines Available
• CokeMachine2000
• Deluxe Model
• Makes Change
• Intermediate Cost
X11
13
Coke Machines Available
• CokeMachineUA
(Urban Assault)
• Secure Model
• Makes Change
• High Cost
• Tamper Proof
X11
13
The Vending Machine Hierarchy
VendingMachine
vend()
load()
vandalize()
toString()
CokeMachine2000
CokeMachine
vend()
vandalize()
vandalize()
toString()
toString()
CokeMachineUA
vandalize()
abstract toString()
X11
13
The Players
• Students
– Normally have change & dollar bills
• Instructors
– Just have change (and not much of that)
• Deans
– Have lots of money
– If machine is out of coke, will vandalize machine
X11
13
The Players
Person
interact()
toString()
abstract
X11
13
Algorithm
• Create population of Students, Instructors and Deans
adding each to population LinkedList
• Create group of machines adding to machines LinkedList
• Loop in time
– Dequeue a person
– Dequeue a machine
– Allow them to interact (capture statistics)
– If the person is still living
• Enqueue them back into population
– Enqueue machine
– If time for service
• Randomly select and service one machine
13 X11
The Simulation
p.interact(v)
p v
population machines
if still
alive
X11
13
Heart of Simulation
p = (Person)population.removeFirst();
v = (VendingMachine)machines.removeFirst();
result = p.interact(v);
stats[result + 2]++;
if(result >= -1)
{
population.addLast(p);
}
else
{
System.out.println("Another one bites the dust!");
}
machines.addLast(v);
13 X11
Without Polymorphism?
• Imagine what would have to happen in interaction without polymorphism
if Student
if CokeMachine
else if CokeMachine2000
else if CokeMachineUA
else if Instructor
if CokeMachine
else if CokeMachine2000
else if CokeMachineUA
elseif Dean
if CokeMachine
else if CokeMachine2000
else if CokeMachineUA
13 X11
Service
if((time % SERVICE) == 0)
{
int chg;
int random;
int siz = machines.size();
VendingMachine vs;
random = (int)(siz * Math.random());
vs = (VendingMachine)machines.get(random);
vs.load(CASE);
chg = STARTUPCHANGE - vs.getChange();
chg = vs.addChange(chg);
startup += chg;
}
X11
13
Demo
X11
13
X11
13
X11
13