82
13X11 13X11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

Embed Size (px)

Citation preview

Page 1: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X1113X11

Java Lecture 5

CS 1311X

Our Story So Far

The Story of O

Inheritance, Polymorphism and Death in the Afternoon

Page 2: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Our Story So Far

• Object Oriented Programming Features– Encapsulation– Reusability– Adaptability

• Object Oriented Programming Benefits– Generic "Drop In" Components– Modeling Real World Objects– Handling Collections Easily

Page 3: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Our Story So Far

• Java Language Syntax• Class Structure

– Fields– Constructors– Methods

• Accessors• Modifiers

– Special Concepts• toString• main• static

Page 4: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Our Story So Far

• All objects are manipulated using references

Page 5: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

The Story of O

CS Version

Page 6: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

The Story of O

Object Oriented

Page 7: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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;

Page 8: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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)

Page 9: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

The Story of O

• Overriding– Also called redefinition

class A {

int someMethod()

}

class B extends A

int someMethod()

}

Page 10: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

The Story of O

Everything

is an

Object!

Page 11: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Inheritanceclass Animal

{

String name;

public Animal(String name)

{

this.name = name;

}

}

Page 12: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Inheritanceclass Dog extends Animal

{

int fleas;

public Dog(String name, int fleas)

{

super(name);

this.fleas = fleas;

}

}

Page 13: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Inheritanceclass Cat extends Animal

{

int hairBalls;

public Cat(String name, int hairBalls)

{

super(name);

this.hairBalls = hairBalls;

}

}

Page 14: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Inheritance (Deceptive Diagram)

Animal

CatDog

Page 15: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Inheritance (True Diagram)

Animal

CatDog

Object

Page 16: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Truth Be Known

• Class Object exists• It defines lots of useful methods

– e.g. toString– see the API

• Thus every class is either – a direct subclass of Object (no extends)

or– a subclass of a descendant of Object (extends)

• So what?

Page 17: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Motivation

• Repetitive tasks

• Collections of things (objects)– Baseball cards– Library items– Shapes– Animals– Vehicles– Students

Page 18: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Motivation

• Collections are seldom uniform

• Desire method of holding a collection of dissimilar items

• Need to change the type mismatch rules

Page 19: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Recall

float f;

double d ;

int i;

String s;

CokeMachine cm;

f = d; // illegal

d = f; // legal

i = d; // illegal

d = i; // legal

Page 20: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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

Page 21: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Recall

float f;

double d ;

int i;

String s;

CokeMachine cm;

s = cm; // illegal

cm = s; // illegal

Page 22: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Recall

float f;

double d ;

int i;

String s;

CokeMachine cm;

s = (String)cm; // illegal

cm = (CokeMachine)s; // illegal

Page 23: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Inheritance Changes the Rules

Animal

CatDog

Object

Page 24: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Inheritance Changes the RulesObject 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?

Page 25: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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

Page 26: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Step by Step

d

Ref: Dog

Dog d

Page 27: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Step by Step

d

Ref: Dog

Dog d = new Dog();public Dog(){

}

Page 28: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Step by Step

d

Ref: Dog

Dog d = new Dog();public Dog(){

}

public Animal(){

}

Page 29: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Step by Step

d

Ref: Dog

Dog d = new Dog();public Dog(){

}

public Animal(){

}

public Object(){

}

Object

Page 30: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Step by Step

d

Ref: Dog

Dog d = new Dog();public Dog(){

}

public Animal(){

}

Object

Animal

Page 31: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Step by Step

d

Ref: Dog

Dog d = new Dog();public Dog(){

}Object

Animal

Dog

Page 32: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Step by Step

d

Ref: Dog

Dog d = new Dog();

Object

Animal

Dog

Page 33: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Step by Step

d

Ref: Dog

Dog d = new Dog();

Object

Animal

Dog

"A Dog Object"

Page 34: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Quizlette

a

Ref: Animal

Animal a = new Dog();

Object

Animal

Dog

"A Dog Object"

Page 35: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Quizlette

o

Ref: Object

Object o = new Dog();

Object

Animal

Dog

"A Dog Object"

Page 36: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Quizlette

d

Ref: Dog

Dog d = new Animal();

Object

Animal "An Animal Object"

ILLEGAL

Page 37: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Quizlette

a

Ref: Animal

Animal a = new Object();

Object

"An Object Object"

ILLEGAL

Page 38: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Quizlette

Dog d = new Object();

?

Page 39: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Quizlette

d

Ref: Dog

Dog d = new Object();

Object

"An Object Object"

ILLEGAL

Page 40: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Same Logic

d

Ref: Dogd = o;

o = d;

o

Ref: Object

o

Ref: Object

d

Ref: Dog

?

?OK!

Page 41: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Don't be confused!!!

• Primitives

double d;

float f;

d = f; // legal...no loss of information

f = d; // illegal...potential loss of

// information

Page 42: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Don't be confused!!!

•References

Object o;

Dog d;

o = d; // legal (a dog is an object)

d = o; // illegal (all objects are not

// dogs)

Note: Not the same as primitives...they are all just references!!!

Page 43: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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;

Page 44: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

What's the Point?

• Java Philosophy: Catch errors at compile time.

• Leading to tricky concept #2: Dynamic Binding

• At run time (dynamic) when a method is invoked on a reference the ACTUAL OBJECT is examined and the "lowest" or closest version of the method is actually run.

Page 45: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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();

Animal

Object

Dog

Animal

Object

Object

Page 46: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Dynamic Binding

• It even works like this...

Object toString()

Animal

Dog toString()

Animal a = new Dog();a.toString();

Ref: Animal

a

A Dog Object

Page 47: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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.

Page 48: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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.

Page 49: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

More Quizlette FunObject o = new Dog();

Animal a = new Dog();

Dog d = new Dog();

o.toString();

o.move();

o.bark();

a.toString();

a.move();

a.bark();

d.toString();

d.move();

d.bark();

Object toString()

Animal move()

Dog move() toString() bark()

Object toString()

Animal move()

Dog move() toString() bark()

Object toString()

Animal move()

Dog move() toString() bark()

d

a

o

Page 50: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Back to Collections

• The simple approach

Object

Dog woof()

Cat meow()

Pig oink()

Page 51: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Java Collections

• LinkedList• ArrayList• Vector• Stack• HashSet• TreeSet• HashTable• Plus you'll write lots on your own...

• They all hold objects!

Page 52: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Using Generic Collections -- PoorlyLinkedList 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();

if(o instanceOf Dog)

((Dog)o).bark();

if(o instanceOf Cat)

((Cat)o).meow();

if(o instanceOf Pig)

((Pig)o).oink();

}

Page 53: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Can We Do Better?

• A first try

Object

Dog talk()

Cat talk()

Pig talk()

Page 54: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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???

}

Page 55: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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???

}

Page 56: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Mystery Partially Solved

Object

Dog talk()

Cat talk()

Pig talk()

Animal talk()

Page 57: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Using Generic Collections -- CorrectlyLinkedList 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();

}

Page 58: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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

*/

}

Page 59: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

How many will forget?

Page 60: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Solution: Abstract Methods and Classesclass Animal

{

public abstract void talk();

// Note: No body!

}

Result: Error!!!

Sometimes called a deferredmethod...

Page 61: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Solution: Abstract Methods and Classesabstract class Animal

{

public abstract void talk();

// Note: No body!

}

Result: Okay.

Page 62: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Java Never Forgets

• Any subclass of class Animal has two choices:

– Define a talk method (i.e. { })

– Be abstract

• Note: Abstract classes may not be used to instantiate or make objects (new)

• References to abstract classes are legal (and quite desireable).

Page 63: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Concreteclass Dog extends Animal

{

public void talk()

{

bark();

}

}

Page 64: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Orclass Dog extends Animal

{

public void talk()

{

System.out.println("Bow wow!);

}

}

Page 65: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Just for the Recordabstract class Canine

{

// define other stuff?

}

Object

Dog talk()

Cat talk()

Pig talk()

abstract Animal abstract talk()

abstract Canine

Wolf talk()

Page 66: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Using Generic Collections -- CorrectlyLinkedList 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();

}

Object

Dog talk()

Cat talk()

Pig talk()

abstract Animal abstract talk()

Page 67: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

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!

Page 68: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Back to the Problem at Hand

Page 69: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Coke Machines Available

•CokeMachine

•Standard Model•Requires Exact Change•Low Cost

Page 70: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Coke Machines Available

•CokeMachine2000

•Deluxe Model•Makes Change•Intermediate Cost

Page 71: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Coke Machines Available

•CokeMachineUA (Urban Assault)

•Secure Model•Makes Change•High Cost

•Tamper Proof

Page 72: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

The Vending Machine Hierarchy

VendingMachine vend() load() vandalize() toString()

CokeMachine vandalize() toString()

CokeMachine2000 vend() vandalize() toString()

CokeMachineUA vandalize() toString()abstract

Page 73: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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

Page 74: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

The Players

Person interact() toString()

Student interact()

Instructor interact()

Dean interact()

abstract

Page 75: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

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

Page 76: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

The Simulation

machinespopulation

p.interact(v)

p v

if stillalive

Page 77: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Heart of Simulationp = (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);

Page 78: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Without Polymorphism?• Imagine what would have to happen in interaction without polymorphism

if Studentif CokeMachineelse if CokeMachine2000else if CokeMachineUA

else if Instructorif CokeMachineelse if CokeMachine2000else if CokeMachineUA

elseif Deanif CokeMachineelse if CokeMachine2000else if CokeMachineUA

Page 79: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Serviceif((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;

}

Page 80: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Demo

Page 81: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11

Page 82: 13 X 11 Java Lecture 5 CS 1311X Our Story So Far The Story of O Inheritance, Polymorphism and Death in the Afternoon

13X11