Escolar Documentos
Profissional Documentos
Cultura Documentos
CS 1311X
Our Story So Far
The Story of O
Inheritance, Polymorphism and Death
in the Afternoon
X11
13
X11
13
Special Concepts
toString
main
static
X11
13
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;
public Animal(String name)
{
this.name = name;
}
}
X11
13
Inheritance
class Dog extends Animal
{
int fleas;
public Dog(String name, int fleas)
{
super(name);
this.fleas = fleas;
}
}
X11
13
Inheritance
class Cat extends Animal
{
int hairBalls;
public Cat(String name, int hairBalls)
{
super(name);
this.hairBalls = hairBalls;
}
}
X11
13
Dog
Cat
X11
13
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
Collections of things (objects)
Baseball cards
Library items
Shapes
Animals
Vehicles
Students
X11
13
Motivation
Collections are seldom uniform
Desire method of holding a collection of dissimilar
items
Need to change the type mismatch rules
X11
13
Recall
float f;
double d ;
int i;
String s;
CokeMachine cm;
f = d;
d = f;
i = d;
d = i;
//
//
//
//
illegal
legal
illegal
legal
X11
13
Recall
float f;
double d ;
int i;
String s;
CokeMachine cm;
f = (float)d;
d = f;
i = (int)d;
d = i;
//
//
//
//
legal
legal
legal
legal
X11
13
Recall
float f;
double d ;
int i;
String s;
CokeMachine cm;
s = cm;
cm = s;
// illegal
// illegal
X11
13
Recall
float f;
double d ;
int i;
String s;
CokeMachine cm;
s = (String)cm;
// illegal
cm = (CokeMachine)s; // illegal
X11
13
Animal
Dog
Cat
X11
13
Dog();
Cat();
// illegal
// OK, a Cat is an Animal
// OK, a Cat is an Object
// OK, an Animal is an Object
// Illegal, not all Objects are Animals
// Illegal, not all animals are Dogs
Confusing?
X11
13
X11
13
Step by Step
Dog d
Ref: Dog
d
X11
13
Step by Step
Dog d = new Dog();
Ref: Dog
public Dog()
{
}
X11
13
Step by Step
Dog d = new Dog();
public Dog()
{
Ref: Dog
public Animal()
{
}
X11
13
Step by Step
Dog d = new Dog();
public Dog()
{
Object
Ref: Dog
public Animal()
{
}
public Object()
{
}
X11
13
Step by Step
Dog d = new Dog();
Object
Ref: Dog
d
Animal
public Dog()
{
}
public Animal()
{
}
X11
13
Step by Step
Dog d = new Dog();
Object
Ref: Dog
Animal
Dog
public Dog()
{
}
X11
13
Step by Step
Dog d = new Dog();
Object
Ref: Dog
Animal
Dog
X11
13
Step by Step
Dog d = new Dog();
Object
Ref: Dog
Animal
Dog
X11
13
Quizlette
Animal a = new Dog();
Object
Ref: Animal
Animal
Dog
X11
13
Quizlette
Object o = new Dog();
Object
Ref: Object
Animal
Dog
X11
13
Quizlette
Dog d = new Animal();
Object
Ref: Dog
Animal
ILLEGAL
X11
13
Quizlette
Animal a = new Object();
Object
Ref: Animal
ILLEGAL
X11
13
Quizlette
Dog d = new Object();
?
X11
13
Quizlette
Dog d = new Object();
Object
Ref: Dog
ILLEGAL
X11
13
Same Logic
d = o;
Ref: Dog
d
Ref: Object
o
Ref: Object
o
Ref: Dog
d
o = d;
OK!
?
X11
13
Don't be confused!!!
Primitives
double d;
float f;
d = f;
f = d;
// illegal...potential loss of
// information
X11
13
Don't be confused!!!
References
Object o;
Dog d;
o = d;
d = o;
// illegal (all objects are not
// dogs)
Note: Not the same as primitives...they are all just
references!!!
X11
13
Warning
When you break the aforementioned rules...
Java sometimes tells you that a cast is required
Even if it's a real bad idea
Pearls p;
Swine s;
p = (Pearls)s;
X11
13
X11
13
Dynamic Binding
The heart of polymorphism
Assume Animal and Dog have a toString method
Object o = new Dog();
Animal a = new Dog();
Dog d = new Dog();
o.toString();
a.toString();
d.toString();
((Object)o).toString();
Object
Object
Animal
Object
Animal
Dog
X11
13
Dynamic Binding
It even works like this...
Object
toString()
Animal
Ref: Animal
a
A Dog
Object
Dog
toString()
X11
13
Trick #3
Java checks types at compile time when
assigning references (Run time checking is also
performed).
Java always decides the method to be invoked by
looking at the object at runtime.
At compile time Java checks method invocations
to make sure that the reference type will have the
correct method. This may appear contradictory to
dynamic binding.
X11
13
Reference/Method Checking
x.y();
x is a reference which has a type which is its class
That class (or a superclass) must have method y
or a compile error will result.
X11
13
Object
toString()
o
a
d
Object
Animal
toString()
move()
Animal
Dog move()
Object
move()toString()
Dog
toString()
Animal
move()
bark()
move()
toString()
bark()
Dog
move()
toString()
bark()
X11
13
Back to Collections
The simple approach
Object
Dog
woof()
Cat
meow()
Pig
oink()
X11
13
Java Collections
LinkedList
ArrayList
Vector
Stack
HashSet
TreeSet
HashTable
Plus you'll write lots on your own...
X11
13
X11
13
Can We Do Better?
A first try
Object
Dog
talk()
Cat
talk()
Pig
talk()
X11
13
X11
13
X11
13
Animal
talk()
Dog
talk()
Cat
talk()
Pig
talk()
X11
13
X11
13
Software Engineering
What should the talk method in class Animal do?
public void talk()
{
/*
* Make sure you redefine this method in
* your individual animal subclasses
*/
}
X11
13
X11
13
Result: Error!!!
X11
13
Result: Okay.
X11
13
X11
13
Concrete
class Dog extends Animal
{
public void talk()
{
bark();
}
}
X11
13
Or
class Dog extends Animal
{
public void talk()
{
System.out.println("Bow wow!);
}
}
X11
13
Object
abstract Animal
abstract talk()
abstract Canine
Dog
talk()
Cat
talk()
Pig
talk()
Wolf
talk()
X11
13
X11
13
Design Guidelines
Class hierarchy design is important
Move common methods up the tree
Use abstract methods appropriately
May be iterative process
X11
13
X11
13
CokeMachine
Standard Model
Requires Exact Change
Low Cost
X11
13
CokeMachine2000
Deluxe Model
Makes Change
Intermediate Cost
X11
13
CokeMachineUA
(Urban Assault)
Secure Model
Makes Change
High Cost
Tamper Proof
X11
13
CokeMachine
vandalize()
toString()
abstract
CokeMachine2000
vend()
vandalize()
toString()
CokeMachineUA
vandalize()
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()
Student
interact()
abstract
Instructor
interact()
Dean
interact()
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
X11
13
The Simulation
p.interact(v)
p
population
v
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);
X11
13
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
X11
13
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