Introduction to Polymorphism - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Polymorphism

Description:

myDog = new Dog(); the Dog constructor gets called. ... Dog. myDog. Dog. Object. reference. Polymorphism Explained. The rule is very simple ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 27
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Polymorphism


1
Introduction to Polymorphism
2
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!

3
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.

4
Motivation
  • Wed like to be able to manage objects of
    different kinds of classes.
  • Since classes within a class hierarchy often
    share common methods and attributes, wed like to
    make use of this fact to make our algorithms
    simpler.

5
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...

6
A Class Hierarchy
Animal
Dog
Cat
Fish
7
A Polymorphic Example
Animal
Dog
Dog myDog myDog new Dog() Animal
myAnimal myAnimal myDog
8
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 heres the big secret

9
Everything is an Object!
  • Thats right!
  • There is a class Object already defined
  • When you dont 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 this

Object
reference
Dog Object
Animal
myDog
Dog
10
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
reference
Dog Object
Animal

Dog
11
Object
Dog
Animal
Object
Dog d d new Dog()
Dog d d new Animal()
Dog d d new Object()
Object
REF
REF
REF
Animal a a new Dog()
Animal a a new Animal()
Animal a a new Object()
Object
REF
REF
REF
Reference
Object o o new Dog()
Object o o new Animal()
Object o o new Object()
Object
REF
REF
REF
Could this be some form of Matrix?
12
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 cant assign a
    superclass object into a sub-class object.
  • Dog MyDog new Animal() // illegal

All dogs are animals but not all animals are dogs
13
Method Calls and Polymorphism
  • Assume 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 its the Dogs makeNoise method that
    gets invoked!

14
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()
Object o
Animal a
A Dog Object
Animal toString()
Dog d
Dog toString()
o.toString() a.toString() d.toString()
15
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()
Object o
Animal a
A Dog Object
Animal
Dog d
Dog toString()
o.toString() a.toString() d.toString()
16
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).

17
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.
18
The Banking Class Hierarchy
Object
BankAccount
SavingsAccount
CheckingAccount
DeluxeSavings
NOWAccount
MoneyMarketAccount
CDAccount
19
A Collection of Bank Accounts
  • Imagine 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.

20
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)

21
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 its own set of capabilities
    via inheritance (extension, and/or redefinition).

22
Example of Polymorphic Banking
  • With polymorphism, our main algorithm doesnt
    care what kind of account it is processing
  • double sum
  • . . .
  • sum 0.0
  • while(! acctQueue.isEmpty())
  • sum
  • ((BankAccount)(acctQueue.dequeue())).getBalan
    ce()
  • System.out.println
  • (Sum of the balances is sum )

23
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 its for our own
    good!
  • Again, Java wants to catch as many problems at
    compile time to avoid errors at run time!

24
Polymorphism
  • This is the magic of polymorphismit keeps
    track of family members within the inheritance
    hierarchy for you.
  • Without it, wed have lots of code sprinkled
    through out our algorithm choosing among many
    options
  • if( its a Checking_Account )
  • call Checking_Account Calc_Interest
  • else if( its a Super_Savings )
  • call Super_Savings Calc_Interest
  • else if( its a CD_Account )
  • call CD_Account Calc_Interest
  • else if( its a NOW_Account )
  • call NOW_Account Calc_Interest
  • . . .

25
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.

26
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com