Lecture 10: Polymorphism

  • Upload
    zhiliu

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 8/20/2019 Lecture 10: Polymorphism

    1/17

    Chapter 10: Polymorphism

    !

  • 8/20/2019 Lecture 10: Polymorphism

    2/17

    Polymorphism, Sorting and Searching

    Polymorphism

    • 

    Polymorphism is an OO concept that allows us to create

    versatile software designs

    •  Chapter 10 focuses on:!

     

    defining polymorphism and its benefits

    !  using inheritance to create polymorphic references

    using interfaces to create polymorphic references

    !  using polymorphism to implement sorting and searching

    algorithms

    additional GUI components

    #$% !&' '

  • 8/20/2019 Lecture 10: Polymorphism

    3/17

    Polymorphism, Sorting and Searching

    Binding

    •   At some point, this call is bound  to the definition of themethod that it calls

    !

     but when?

    !  If at compile time, it would call the same method every time

    If at runtime, it could be bound to a method "on the fly"

    • 

    Java chooses the latter – it's called dynamic binding orlate binding

    •  Late binding is the basis for polymorphism ! providesflexibility in program design

    #$% !&' (

    student.computeGrade();

     

    Consider this method call:

  • 8/20/2019 Lecture 10: Polymorphism

    4/17

    Polymorphism, Sorting and Searching

    Polymorphism

    • 

    Polymorphism literally means "having many forms"

    •   A polymorphic reference is a variable that can refer todifferent types of objects at different times

    •  Which method is called through a polymorphic referencecan change from one call to the next

    #$% !&' )

    All object references in Javaare potentially polymorphic

  • 8/20/2019 Lecture 10: Polymorphism

    5/17

    Polymorphism, Sorting and Searching

    Polymorphism

    • 

    Java allows job to point to:

    an Occupation object, or

    !  an object of any compatible type

    • 

    This compatibility can be based on inheritance orinterfaces! as we'll see! 

    #$% !&' &

    Occupation job;

    • 

    Given this declaration:

  • 8/20/2019 Lecture 10: Polymorphism

    6/17

    Polymorphism, Sorting and Searching

    References and Inheritance

    • 

    Consider this example

    #$% !&' *

    Bicycle

    RoadBike MountainBike

    •  Suppose Bicycle has a method called printDescription

    • 

     And that RoadBike and MountainBike override this method

    with customized versions

  • 8/20/2019 Lecture 10: Polymorphism

    7/17

    Polymorphism, Sorting and Searching

    References and Inheritance

    • 

    Examine this code:

    #$% !&' +

    public class TestBikes

    {

    public static void main(String[] args)

    {

    Bicycle bike1, bike2, bike3;bike1 = new Bicycle(20, 10, 1);

    bike2 = new MountainBike(20, 10, 5, "Dual");

    bike3 = new RoadBike(40, 20, 8, 23);

    bike1.printDescription();

    bike2.printDescription();bike3.printDescription();

    }

    }

    Who ya gonna call?

  • 8/20/2019 Lecture 10: Polymorphism

    8/17

    Polymorphism, Sorting and Searching

    References and Inheritance

    #$% !&' ,

    Bicycle bike3;

    bike3 = new RoadBike (40, 20, 8, 23 );

    MountainBike bike2 ;

    bike2 = (MountainBike) new Bicycle (20,10,1);

    Bicycle

    RoadBike MountainBike

    Wideningconversion

    (no problem)

    Narrowing

    conversion

    (needs cast)

  • 8/20/2019 Lecture 10: Polymorphism

    9/17

    Polymorphism, Sorting and Searching

    References and Inheritance

    #$% !&' -

    public class TestBikes

    {

    public static void main(String[] args)

    {Bicycle bike1, bike2, bike3;

    bike1 = new Bicycle(20, 10, 1);

    bike2 = new MountainBike(20, 10, 5, "Dual");

    bike3 = new RoadBike(40, 20, 8, 23);

    bike1.printDescription();

    bike2.printDescription();

    bike3.printDescription();

    }

    }

    • 

    It is the type of the object being referenced, not thereference type, that determines which method is invoked

  • 8/20/2019 Lecture 10: Polymorphism

    10/17

    Polymorphism, Sorting and Searching

    Polymorphism via Inheritance

    • 

    Consider the task ofpaying employees:

    #$% !&' !.

    StaffMember

    Executive Hourly

    Volunteer Employee

    (Abstract class)

    See Firm example (pg. 490-501)

  • 8/20/2019 Lecture 10: Polymorphism

    11/17

    Polymorphism, Sorting and Searching

    Polymorphism via Interfaces

    • 

    Suppose we have an interface called Speaker  whichprescribes a method called speak 

    •  The interface name can be used as a type:

    #$% !&' !!

    Speaker  current;

    •  The variable current can point to any object of any classthat implements Speaker

    current.speak();

    Who ya gonna call?

    •  Which speak method is calleddepends on the type of object

    that current is pointing to at that

    time.

  • 8/20/2019 Lecture 10: Polymorphism

    12/17

    Polymorphism, Sorting and Searching

    Polymorphism via Interfaces

    • 

    Suppose two classes, Philosopher  and Dog, bothimplement the Speaker  interface, and provide distinct

    versions of the speak method:

    #$% !&' !'

    yada yada!

     Speaker guest = new Philospher ( );

    guest.speak ( );

    guest = new Dog ( );

    guest.speak ( );

    Squirrel !!

  • 8/20/2019 Lecture 10: Polymorphism

    13/17

    Polymorphism, Sorting and Searching

    Sorting

    • 

    Sorting arranges a list of items in a particular order

    •  The sorting process is based on specific value(s)!  Sort test scores in ascending numeric order

    Sort people alphabetically by last name

    •  Many sorting algorithms:!  Vary in efficiency and complexity

    • 

    Will look at two simple algorithms:!

     

    Selection Sort

    !  Insertion Sort

    #$% !&' !(

  • 8/20/2019 Lecture 10: Polymorphism

    14/17

    Polymorphism, Sorting and Searching

    Selection Sort

    • 

    General approach:!

     

    Select a value and put it in its final place into the list

    !  Repeat for all other values

    #$% !&' !)

    Selection Sort Algorithm

    •  find the smallest value in the list

    •  switch it with the value in the first position

    • 

    find the next smallest value in the list

    • 

    switch it with the value in the second position

    •  repeat until all values are in their proper places

  • 8/20/2019 Lecture 10: Polymorphism

    15/17

    Polymorphism, Sorting and Searching

    Selection Sort

    • 

     An example:

    #$% !&' !&

    • 

    Each time, the smallest remaining value is found and

    swapped with the element at the front of the unsorted part

    of the list

    3 9 6 1 2

    1  9 6 3  2

    1 2  6 3 9

    1 2 3  6  9

    1 2 3 6  9

    select smallest:

    swap with 1st, select smallest:

    swap with 2nd, select smallest:

    swap with 3rd, select smallest:

    6 ok (don't swap), only one left:

    select

  • 8/20/2019 Lecture 10: Polymorphism

    16/17

    Polymorphism, Sorting and Searching

    Swapping

    • 

    By "swap", we mean exchange the contents of two

    variables (or array elements):

    #$% !&' !*

    temp = blueBucket;blueBucket = redBucket;

    redBucket = temp;

    temp

    1

    2

    3

  • 8/20/2019 Lecture 10: Polymorphism

    17/17

    Polymorphism, Sorting and Searching

    END OF THIS LECTURE!

    Have a Coke and a smile – or whatever . . .

    #$% !&' !+