Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein."— Presentation transcript:

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

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

3 Polymorphism §from the Greek §poly = many §morph = forms

4 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 examplesexamples

5 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

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

7 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

8 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

9 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

10 Polymorphism via Inheritance §Consider the following class hierarchy: StaffMember ExecutiveHourly VolunteerEmployee See page 485 for the full UML diagram

11 Polymorphism via Inheritance §Now consider the task of paying all employees  See Firm.java (page 486) Firm.java  See Staff.java (page 487) Staff.java  See StaffMember.java (page 489) StaffMember.java  See Volunteer.java (page 491) Volunteer.java  See Employee.java (page 492) Employee.java  See Executive.java (page 493) Executive.java  See Hourly.java (page 494) Hourly.java

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

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

14 Meanings of Polymorphism  In this course:  At run-time, a variable may "take the form of" any of its descendants.  Related Concepts: Dynamic binding

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


Download ppt "Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein."

Similar presentations


Ads by Google