lecture 10 – polymorphism nancy harris with additional slides professor adams from lewis &...

15
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Upload: rachel-short

Post on 03-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Lecture 10 – Polymorphism

Nancy Harris with additional slides Professor Adams

from Lewis & Bernstein

Page 2: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

NOTE: PA2

To deal with the command line argument, you do not need a try/catch block.

Exceptions handling should be used for exceptional situations, not for things that you can solve more elegantly in other ways.

Page 3: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Polymorphism

from the Greekpoly = manymorph = forms

Page 4: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

We have seen polymorphism

Think clocks.You can create a Clock or an AlarmClock.You can create a Clock and instantiate an

AlarmClock.The method used (like the updateTime method)

will be based on the object type.But the object declaration determines which

methods are available to us. See examples

Page 5: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Polymorphism A reference can be polymorphic, which can be defined as

"having many forms"

obj.doIt();

This line of code might execute different methods at different times if the object that obj points to changes

Page 6: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Polymorphism

Polymorphic references are resolved at run time; this is called dynamic binding

Careful use of polymorphic references can lead to elegant, robust software designs

Polymorphism can be accomplished using inheritance or using interfaces(to be discussed later)

Page 7: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

7

References and Inheritance

An object reference can refer to an object of its class, or to an object of any class related to it by inheritance

For example, if the Holiday class is used to derive a child class called Christmas, then a Holiday reference could be used to point to a Christmas object

Holiday day;day = new Christmas();

Holiday

Christmas

Page 8: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

8

References and Inheritance

An Object reference can be used to refer to any object

An ArrayList is designed to hold Object references

See the ArrayList in the Appendix M – pg 738

Page 9: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Polymorphism via Inheritance

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

Suppose the Holiday class has a method called celebrate, and the Christmas class overrides it

Now consider the following invocation:

day.celebrate();

If day refers to a Holiday object, it invokes the Holiday version of celebrate; if it refers to a Christmas object, it invokes the Christmas version

Page 10: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Polymorphism via Inheritance

Consider the following class hierarchy:StaffMembe

r

Executive Hourly

Volunteer Employee

See page 485 for the full UML diagram

Page 11: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Polymorphism via Inheritance

Now consider the task of paying all employees

See Firm.java (page 486)See Staff.java (page 487)See StaffMember.java (page 489)See Volunteer.java (page 491)See Employee.java (page 492)See Executive.java (page 493)See Hourly.java (page 494)

Page 12: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Object Class

The Granddaddy of all classes.We override the toString and equals methods

commonly in creating our own classes.Question – Is it possible for us to write code that

will do something different based on the class of the object instead of the class in which the variable is declared. Like, can we prevent the run time errors we saw in the Executive vs Hourly classes.

Page 13: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Class class

Object class has a getClass method that returns the Class of the object.

The Class class has a getName method which returns a String equivalent to the name of the Class.

Using these two, we could write code that will do one thing for one class of object and another for a different class of object.

Page 14: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

Meanings of Polymorphism

In this course: At run-time, a variable may "take the

form of" any of its descendants. Related Concepts:

Dynamic binding

Page 15: Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein

•Polymorphism in Java

Class Membership The compiler uses the declared class At run-time an object knows its class         

Search Order: • If the "message" is sent to an object of the derived class then

the derived class is searched (for the existence of such a method) first and the base class is searched second. (Note: The search will move up the class hierarchy until as needed.)

• If the "message" is sent to an object of the base class then only the base class is searched.