DOC PREVIEW
UNF COP 2551 - Polymorphism

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 9PolymorphismOutlineBinding – Very Important!!Slide 5Slide 6Slide 7References and Inheritance References and InheritancePolymorphism via InheritanceSlide 11MoreSlide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Polymorphism via InterfacesSlide 24Chapter 9Polymorphism© 2004 Pearson Addison-Wesley. All rights reserved 249-2Polymorphism•Polymorphism is an object-oriented concept that allows us to create versatile software designs•Chapter 9 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© 2004 Pearson Addison-Wesley. All rights reserved 249-3OutlinePolymorphic ReferencesPolymorphism via InheritancePolymorphism via InterfacesSortingSearching© 2004 Pearson Addison-Wesley. All rights reserved 249-4Binding – Very Important!!•Consider the following method invocation:obj.doIt(); At some point, this invocation is bound to the definition of the method that it invokes •DISCUSS BINDING!!•If this binding occurred at compile time (and this is what we are used to…), then that line of code would call the same method every time However, Java defers method binding until run time -- this is called dynamic binding or late binding. This is an extremely important feature!!!•Late binding provides flexibility in program design© 2004 Pearson Addison-Wesley. All rights reserved 249-5Polymorphism•The term polymorphism literally means "having many forms"•A polymorphic reference is a variable that can refer to different types of objects at different points in time – despite what the source code looks like!•A method invoked via a polymorphic reference can change from one invocation to the next, that is the reference can invoke a ‘different’ object’s method (with same name) at different points in time! Restated: Consider Obj.doit; With dynamic binding, this may refer to different methods in different objects at different points during execution! All methods will have the name, doit, but the objects that they are in may vary!) All object references in Java are potentially polymorphic due to late binding (dynamic binding)© 2004 Pearson Addison-Wesley. All rights reserved 249-6Polymorphism•Suppose we create the following reference variable:Occupation job;•As written, this creates a ‘reference’ to an object (not yet created) of type Occupation. Java allows this reference to point to an Occupation object (which we know), or to any object of any ‘compatible’ type (like derived types of Occupation!) This is new! So the reference can refer to the job object or any objects inheriting from Occupation. This “compatibility” can be established using inheritance or using interfaces•Careful use of polymorphic references can lead to elegant, robust software designs© 2004 Pearson Addison-Wesley. All rights reserved 249-7OutlinePolymorphic ReferencesPolymorphism via InheritancePolymorphism via Interfaces© 2004 Pearson Addison-Wesley. All rights reserved 249-8References and Inheritance As stated, 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 class called Christmas, then a Holiday reference (‘day’, in this case) could be used to point to a Christmas object. This means that day could point to an object of type Holiday or an object of any derived type!Holiday day; // day is a ref to Holiday object (Holiday object not yet created)day = new Christmas(); // Here, day is an object of type Christmas, a derived class of Holiday. day points to Christmas here. day is a reference to the ‘base’ class. HolidayChristmas© 2004 Pearson Addison-Wesley. All rights reserved 249-9 References and Inheritance•Assigning a child object to a parent reference is considered to be a widening conversion, and can be performed by simple assignmentthis is exactly what we did in the last slide.(a pointer to the base class is actually pointing to an object of a derived type.)  The opposite: Assigning an parent object to a child reference can be done also, but it is considered a narrowing conversion and must be done with a cast The widening conversion is the more useful© 2004 Pearson Addison-Wesley. All rights reserved 249-10Polymorphism via Inheritance  In polymorphism: It is the type of the object being referenced, not the reference type, that determines which method is invoked. •In previous slides, day is pointing to an object of type Christmas! Object being referenced was of type Christmas; Reference type was of type Holiday.•It is very common to have a pointer to a base class (Holiday here) pointing to a derived object (day, of type Christmas here).© 2004 Pearson Addison-Wesley. All rights reserved 249-11•So, we could have the Holiday day; entry Says, day is a reference to objects of type Holiday•And later in code,day = new Christmas;day = new Easter;day = new Thanksgiving;•And then, this reference is bound during run time depending on the program logic; that is, depending on what path in the program is taken and which of the three ‘day’ references are executed.© 2004 Pearson Addison-Wesley. All rights reserved 249-12More•Suppose the Holiday class has a method called celebrate, and the Christmas class overrides it; that is, Christmas also has a method called celebrate.•Now consider the following invocation:day.celebrate(); If (during execution) day refers to (points to) a Holiday object, it invokes the Holiday version of celebrate; if it refers to a Christmas object, it invokes the Christmas version•The program logic will determine this during the execution of the program.© 2004 Pearson Addison-Wesley. All rights reserved 249-13•Let’s consider a practical example from your book.•Be sure to go through this to understand it.© 2004 Pearson Addison-Wesley. All rights reserved 249-14Polymorphism via Inheritance•Consider the following class hierarchy: (Discuss)StaffMemberExecutive HourlyVolunteer Employee© 2004 Pearson Addison-Wesley. All rights reserved 249-15/********************************************************************// Firm.java Author: Lewis/Loftus//// Demonstrates polymorphism via


View Full Document

UNF COP 2551 - Polymorphism

Download Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?