introduction to polymorphism

26
Introduction to Polymorphism

Upload: sybil

Post on 12-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Polymorphism. Recall. We set out to explore a new language, Java, which uses the Object Oriented Paradigm. We claimed that it would allow us to have superior Encapsulation Reusability Adaptability But as of now that may not be quite so apparent. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Polymorphism

Introduction to Polymorphism

Page 2: Introduction to Polymorphism

Recall• We set out to explore a new language, Java, which

uses the Object Oriented Paradigm.• We claimed that it would allow us to have superior

– Encapsulation– Reusability– Adaptability

• But as of now that may not be quite so apparent.• The LinkedList class that we made held a linked list of

StudentRecords but to hold anything else it will require some not insignificant amount of modification

• We need another tool to allow us to make collections that will hold anything!

Page 3: Introduction to Polymorphism

Scenarios• A veterinarian's algorithm might have a list of animals,

but each one needs different food or care… we want ONE information system to track all of this without complex logic for each individual kind of animal.

• A car dealership sells many different types of cars with different features, but each has a price and quantity in stock.

• A registration system might treat in-state students differently from out-of-state students, graduate students differently from undergraduates, etc.

Page 4: Introduction to Polymorphism

Motivation• We’d like to be able to manage objects of different

kinds of classes.

• Since classes within a class hierarchy often share common methods and attributes, we’d like to make use of this fact to make our algorithms simpler.

Page 5: Introduction to Polymorphism

Polymorphism• Polymorphism means many or multiple forms.

• Refers to the ability to tell an object to perform a method without knowing exactly which method is going to get invoked.

• May initially seem somewhat magical

• Well, actually it is...

Page 6: Introduction to Polymorphism

A Class Hierarchy

Animal

Dog Cat Fish

Page 7: Introduction to Polymorphism

A Polymorphic Example

Animal

Dog Dog myDog;myDog = new Dog();

Animal myAnimal;

myAnimal = myDog;

Page 8: Introduction to Polymorphism

Extends• Note that when Java uses inheritance the keyword is

extends• This is because literally making a subclass object in a

sense also makes a superclass object!!!• Huh?• When we say:

myDog = new Dog();• the Dog constructor gets called.• It, in turn, must call the Animal constructor• And here’s the big secret

Page 9: Introduction to Polymorphism

Everything is an Object!• That’s right!• There is a class Object already defined• When you don’t extend anything by default you extend

Object• Thus the Animal constructor calls the Object

constructor• Looking at an object in memory it would look

something like thisObject

Animal

DogmyDog

DogObject

reference

Page 10: Introduction to Polymorphism

Polymorphism Explained• The rule is very simple• A reference can refer to an object which is either

– The same type as the reference– Has a superclass of the same type as the reference

• So all of the following are legal• Dog d = new Dog();• Animal a = new Animal();• Object o = new Object;

Object

Animal

Dog

DogObject

reference

Page 11: Introduction to Polymorphism

Dog d;d = new Dog();

Animal a;a = new Dog();

Object o;o = new Dog();

Dog d;d = new Animal();

Animal a;a = new Animal();

Object o;o = new Animal();

Dog d;d = new Object();

Animal a;a = new Object();

Object o;o = new Object();

Dog Animal ObjectRe

fere

nce

Object

ObjectObject

Animal

Dog

Object

Animal

Dog

Object

Animal

Dog

Object

Animal

Object

Animal

Object

Animal

Object

Object

REF

REF

REF REF

REF

REF REF

REF

REF

Could this be some form of Matrix®?

Dog

Animal

Object

Page 12: Introduction to Polymorphism

An Illegal Example• We are able to assign an object of a sub-class into

an object of a super-class as in:Animal MyAnimal = new Dog();

• But the reverse is not true. We can’t assign a superclass object into a sub-class object. Dog MyDog = new Animal(); // illegal

All dogs are animals butnot all animals are dogs

Page 13: Introduction to Polymorphism

Method Calls and PolymorphismAssume the Dog class extends the Animal class, redefining the “makeNoise” method.

Consider the following:

Animal MyAnimal = new Dog();MyAnimal.makeNoise();

Note: The Animal reference is referring to a Dog object. And it’s the Dog’s makeNoise method that gets invoked!

Page 14: Introduction to Polymorphism

Dynamic Binding• Very simple rule.• No matter what the reference type is, Java will search

the object and execute the lowest occurence of a method it finds.

• class Object has a toString method• Assume that both Animal and Dog have overridden the

toString method Object toString()

Animal toString()

Dog toString()

Object o

Animal a

Dog d A

DogObject

o.toString();a.toString();d.toString();

Page 15: Introduction to Polymorphism

Dynamic Binding• Very simple rule.• No matter what the reference type is, Java will search

the object and execute the lowest occurrence of a method it finds.

• class Object has a toString method• Even if Animal has no toString method!

Object toString()

Animal

Dog toString()

Object o

Animal a

Dog d A

DogObject

o.toString();a.toString();d.toString();

Page 16: Introduction to Polymorphism

Polymorphism vs. Inheritance• Inheritance is required in order to achieve

polymorphism (we must have class hierarchies).– Re-using class definitions via extension and redefinition

• Polymorphism is not required in order to achieve inheritance.– An object of class A acts as an object of class B (an ancestor

to A).

Page 17: Introduction to Polymorphism

Processing Collections• One of the main benefits of polymorphism is the

ability to easily process collections.

• We will consider a collection (queue) of Objects in the next example. . .

Note: This means we rewrite our Queue class(and all associated classes) to hold Objects.

Page 18: Introduction to Polymorphism

The Banking Class Hierarchy

DeluxeSavings

BankAccount

SavingsAccount CheckingAccount

NOWAccount MoneyMarketAccount

CDAccount

Object

Page 19: Introduction to Polymorphism

A Collection of Bank AccountsImagine a bank needs to manage all of the different types of accounts.

Rather than maintain seven separate queues, one each for: BankAccounts, SavingsAccounts, DeluxeSavings, CDAccounts, CheckingAccounts, NOWAccounts, and MoneyMarketAccounts

We can maintain only one queue of Objects.

Page 20: Introduction to Polymorphism

Polymorphic Banking // Assume accounts of various kinds: CheckingAccount johnAaccount; DeluxeSavings paulAccount; CDAccount paulOtherAccount; NOWAccount georgeAccount; MoneyMarket ringoAccount;

// Then put them all in a single structure // which is a Queue which holds Objects Queue acctQueue;

acctQueue.enqueue(johnAccount); acctQueue.enqueue(paulAccount); acctQueue.enqueue(paulOtherAccount); acctQueue.enqueue(georgeAccount); acctQueue.enqueue(ringoAccount);

Page 21: Introduction to Polymorphism

Polymorphic Banking accountQueue is polymorphic:

• It is holding accounts of “many forms.”• Each of the accounts is “within the family” of the

class hierarchy of bank accounts.• Each one will have it’s own set of capabilities via

inheritance (extension, and/or redefinition).

Page 22: Introduction to Polymorphism

Example of Polymorphic BankingWith polymorphism, our main algorithm doesn’t care what kind of account it is processing

double sum;. . .sum = 0.0;while(! acctQueue.isEmpty()) { sum += ((BankAccount)(acctQueue.dequeue())).getBalance();}

System.out.println(“Sum of the balances is: ” + sum );

Page 23: Introduction to Polymorphism

Resolving Polymorphic Method Calls• The Java compiler is our friend• Since we said the Queue was a Queue which was

written to hold Objects what comes out is an Object reference

• class Object has no getBalance() method so the compiler would give us a friendly error message politely refuse to compile until we cast the object to a BankAccount object

• As annoying as this may seem it’s for our own good!• Again, Java wants to catch as many problems at

compile time to avoid errors at run time!

Page 24: Introduction to Polymorphism

Polymorphism• This is the “magic” of polymorphism…it keeps

track of family members within the inheritance hierarchy for you.

• Without it, we’d have lots of code sprinkled through out our algorithm choosing among many options:

if( it’s a Checking_Account ) call Checking_Account Calc_Interest else if( it’s a Super_Savings ) call Super_Savings Calc_Interest else if( it’s a CD_Account ) call CD_Account Calc_Interest else if( it’s a NOW_Account ) call NOW_Account Calc_Interest . . .

Page 25: Introduction to Polymorphism

Summary• Polymorphism allows objects to represent instances of

its own class and any of its subclasses.

• Polymorphic collections are useful for managing objects since all objects are descendants of class Object

• No matter what the reference type the actual object knows what type it is

• When there is a choice dynamic binding goes to the lowest level.

Page 26: Introduction to Polymorphism