Chapter 9 Polymorphism.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Polymorphism Chapter 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 9: Polymorphism Coming up: Binding.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Polymorphism Polymorphism (having many forms) is an object- oriented concept.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
1 Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L 9.1 – 9.3.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Abstract Classes.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter Day 22. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 22 Problem set 4 Posted  10 problems from chapters 7 & 8  Due Nov 21 (right.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Polymorphism  A reference can be polymorphic, which can be defined as "having many forms" obj.doIt();  This line of code might execute different methods.
© 2006 Pearson Education Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
Dr. Sahar Shabanah Lecture 2. Outline Review of O-O Concepts Inheritance Interfaces Polymorphism Casting Generics Java Documentation Exceptions 2.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Chapter 9 Polymorphism. Chapter Scope The role of polymorphism Dynamic binding Using inheritance for polymorphism Exploring Java interfaces in more detail.
COS 312 DAY 14 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress Report Over due – Next progress report is March 26 Assignment 4 Due Assignment 5.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
ELC 310 Day 19. © 2004 Pearson Addison-Wesley. All rights reserved9-2 Agenda Questions? Capstone Proposals Overdue  4 Submitted  3 Accepted, 1 in negotiations.
Java Applets and other things. Applets Very like usual classes Use init() method instead of main Applets provide 4 methods init() start() stop() destroy()
1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
CS102 Algorithims and Programming II1 Inheritance Inheritance defines a relationship among classes. A class C2 may inherit from (extend – derive from)
COS 312 DAY 16 Tony Gauvin.
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
03/10/14 Inheritance-2.
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Abdulmotaleb El Saddik University of Ottawa
Polymorphism.
Chapter 7: Inheritance Java Software Solutions
Object-Oriented Programming: Polymorphism
Designing for Inheritance
Review for the Final Exam
Polymorphism 2nd Lecture
Review of Object-Oriented Concepts in JAVA
CMSC 202 Polymorphism.
Review of Object-Oriented Concepts in JAVA
Chapter 8 Inheritance Part 2.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Chapter 9 Polymorphism

Polymorphism Polymorphism is an object-oriented 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 © 2004 Pearson Addison-Wesley. All rights reserved

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces © 2004 Pearson Addison-Wesley. All rights reserved

Binding Consider the following method invocation: obj.doIt(); At some point, this invocation is bound to the definition of the method that it invokes If this binding occurred at compile time, 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 Late binding provides flexibility in program design © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism 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 The method invoked through a polymorphic reference can change from one invocation to the next All object references in Java are potentially polymorphic © 2004 Pearson Addison-Wesley. All rights reserved

Example public abstract class Animal { // class is abstract private String name; public Animal(String nm) { // constructor method name=nm; } public String getName() { // regular method return (name); public abstract void speak(); // abstract method – no {} Three subclasses (Cow, Dog and Snake) each having their own speak() method. http://home.cogeco.ca/~ve3ll/jatutor5.htm

Example - Late Method Binding public class AnimalReference { public static void main(String args[]) { Animal ref; // set up var for Animal abstract class Cow aCow = new Cow("Bossy"); // makes specific objects Dog aDog = new Dog("Rover"); // from the subclasses Snake aSnake = new Snake("Ernie"); // now reference each as an Animal ref = aCow; ref.speak(); ref = aDog; ref.speak(); // resolve at run-time ref = aSnake; ref.speak(); } http://home.cogeco.ca/~ve3ll/jatutor5.htm

Example - Array of Objects public class AnimalArray { public static void main(String args[]) { Animal ref[] = new Animal[3]; Cow aCow = new Cow("Bossy"); Dog aDog = new Dog("Rover"); Snake aSnake = new Snake("Earnie"); // now put them in an array ref[0] = aCow; ref[1] = aDog; ref[2] = aSnake; // now demo dynamic method binding for (int x=0;x<3;++x) { ref[x].speak(); } } http://home.cogeco.ca/~ve3ll/jatutor5.htm

Casting Objects Dog doggy = (Dog) ref[x]; //cast current instance to // subclass doggy.someDogOnlyMethod(); Casting an individual instance to its subclass form, one can refer to any property or method http://home.cogeco.ca/~ve3ll/jatutor5.htm

Inheritance and References class C1 { ... } class C2 extends C1 { ... } class C3 { ... } C1 x; // x can store a reference to an object of C1 or // an object of any subclass of C1. // in some other place C1 o1 = new C1(); C2 o2 = new C2(); C3 o3 = new C3(); CS102 Algorithims and Programming II

Inheritance and References (cont.) class C1 { ... } class C2 extends C1 { ... } class C3 { ... } C1 o1 = new C1(); C2 o2 = new C2(); C3 o3 = new C3(); o1 = o2; // Automatically saved (Widening Conversion) o2 = (C2) o1; // We need explicit type casting (Narrowing Conversion) if o1 holds C2 object, this is okay; But, if o1 holds C1 object  run-time error o1 = o3; // ILLEGAL (no inheritance relation between C1 and C3) o1 = (C1) o3; // ILLEGAL (no inheritance relation between C1 and C3) CS102 Algorithims and Programming II

Inheritance and References (cont.) Assigning a child reference to a parent reference is a widening conversion, and it can be performed by simple assignment. o1 = o2; ref = aCow; Assigning a parent reference to a child reference is a narrowing conversion, and it must be done with an explicit type cast operation. o2 = (C2) o1; If that parent reference (o1) does not point to a child object (o2)  run-time error Since Object class is the ancestor of all classes in Java, we can store any type of reference in an Object reference variable CS102 Algorithims and Programming II

Inheritance and References (cont.) class C1 { ... } class C2 extends C1 { ... } // in some other place C1 o1 = new C1(); C2 o2 = new C2(); Object o3; o3 = o1; Automatically saved (Widening Conversion) o3 = o2; Automatically saved (Widening Conversion) CS102 Algorithims and Programming II

Polymorphism Suppose we create the following reference variable: Occupation job; Java allows this reference to point to an Occupation object, or to any object of any compatible type 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

© 2004 Pearson Addison-Wesley. All rights reserved

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces © 2004 Pearson Addison-Wesley. All rights reserved

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 class called Christmas, then a Holiday reference could be used to point to a Christmas object Holiday Christmas Holiday day; day = new Christmas(); © 2004 Pearson Addison-Wesley. All rights reserved

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 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 most useful © 2004 Pearson Addison-Wesley. All rights reserved

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 © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism via Inheritance Consider the following class hierarchy: StaffMember Executive Hourly Volunteer Employee © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism via Inheritance Now let's look at an example that pays a set of diverse employees using a polymorphic method See Firm.java (Chapter 10.2, page 518) See Staff.java (Chapter 10.2, page 519) See StaffMember.java (Chapter 10.2, page 521) See Volunteer.java (Chapter 10.2, page 522) See Employee.java (Chapter 10.2, page 524) See Executive.java (Chapter 10.2, page 525) See Hourly.java (Chapter 10.2, page 526) © 2004 Pearson Addison-Wesley. All rights reserved

//******************************************************************** // Firm.java Author: Lewis/Loftus // // Demonstrates polymorphism via inheritance. public class Firm { //----------------------------------------------------------------- // Creates a staff of employees for a firm and pays them. public static void main (String[] args) Staff personnel = new Staff(); personnel.payday(); } Copyright © 2012 Pearson Education, Inc.

Output Output (continued) Name: Sam Address: 123 Main Line Phone: 555-0469 Social Security Number: 123-45-6789 Paid: 2923.07 ----------------------------------- Name: Carla Address: 456 Off Line Phone: 555-0101 Social Security Number: 987-65-4321 Paid: 1246.15 Name: Woody Address: 789 Off Rocker Phone: 555-0000 Social Security Number: 010-20-3040 Paid: 1169.23 Output (continued) Name: Diane Address: 678 Fifth Ave. Phone: 555-0690 Social Security Number: 958-47-3625 Current hours: 40 Paid: 422.0 ----------------------------------- Name: Norm Address: 987 Suds Blvd. Phone: 555-8374 Thanks! Name: Cliff Address: 321 Duds Lane Phone: 555-7282 //******************************************************************** // Firm.java Author: Lewis/Loftus // // Demonstrates polymorphism via inheritance. public class Firm { //----------------------------------------------------------------- // Creates a staff of employees for a firm and pays them. public static void main (String[] args) Staff personnel = new Staff(); personnel.payday(); } Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Staff.java Author: Lewis/Loftus // // Represents the personnel staff of a particular business. public class Staff { private StaffMember[] staffList; //----------------------------------------------------------------- // Constructor: Sets up the list of staff members. public Staff () staffList = new StaffMember[6]; continue Copyright © 2012 Pearson Education, Inc.

staffList[0] = new Executive ("Sam", "123 Main Line", continue staffList[0] = new Executive ("Sam", "123 Main Line", "555-0469", "123-45-6789", 2423.07); staffList[1] = new Employee ("Carla", "456 Off Line", "555-0101", "987-65-4321", 1246.15); staffList[2] = new Employee ("Woody", "789 Off Rocker", "555-0000", "010-20-3040", 1169.23); staffList[3] = new Hourly ("Diane", "678 Fifth Ave.", "555-0690", "958-47-3625", 10.55); staffList[4] = new Volunteer ("Norm", "987 Suds Blvd.", "555-8374"); staffList[5] = new Volunteer ("Cliff", "321 Duds Lane", "555-7282"); ((Executive)staffList[0]).awardBonus (500.00); ((Hourly)staffList[3]).addHours (40); } Copyright © 2012 Pearson Education, Inc.

//----------------------------------------------------------------- continue //----------------------------------------------------------------- // Pays all staff members. public void payday () { double amount; for (int count=0; count < staffList.length; count++) System.out.println (staffList[count]); amount = staffList[count].pay(); // polymorphic if (amount == 0.0) System.out.println ("Thanks!"); else System.out.println ("Paid: " + amount); System.out.println ("-----------------------------------"); } Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // StaffMember.java Author: Lewis/Loftus // // Represents a generic staff member. abstract public class StaffMember { protected String name; protected String address; protected String phone; //----------------------------------------------------------------- // Constructor: Sets up this staff member using the specified // information. public StaffMember (String eName, String eAddress, String ePhone) name = eName; address = eAddress; phone = ePhone; } continue Copyright © 2012 Pearson Education, Inc.

//----------------------------------------------------------------- continue //----------------------------------------------------------------- // Returns a string including the basic employee information. public String toString() { String result = "Name: " + name + "\n"; result += "Address: " + address + "\n"; result += "Phone: " + phone; return result; } // Derived classes must define the pay method for each type of // employee. public abstract double pay(); Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Volunteer.java Author: Lewis/Loftus // // Represents a staff member that works as a volunteer. public class Volunteer extends StaffMember { //----------------------------------------------------------------- // Constructor: Sets up this volunteer using the specified // information. public Volunteer (String eName, String eAddress, String ePhone) super (eName, eAddress, ePhone); } // Returns a zero pay value for this volunteer. public double pay() return 0.0; Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Employee.java Author: Lewis/Loftus // // Represents a general paid employee. public class Employee extends StaffMember { protected String socialSecurityNumber; protected double payRate; //----------------------------------------------------------------- // Constructor: Sets up this employee with the specified // information. public Employee (String eName, String eAddress, String ePhone, String socSecNumber, double rate) super (eName, eAddress, ePhone); socialSecurityNumber = socSecNumber; payRate = rate; } continue Copyright © 2012 Pearson Education, Inc.

//----------------------------------------------------------------- continue //----------------------------------------------------------------- // Returns information about an employee as a string. public String toString() { String result = super.toString(); result += "\nSocial Security Number: " + socialSecurityNumber; return result; } // Returns the pay rate for this employee. public double pay() return payRate; Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Executive.java Author: Lewis/Loftus // // Represents an executive staff member, who can earn a bonus. public class Executive extends Employee { private double bonus; //----------------------------------------------------------------- // Constructor: Sets up this executive with the specified // information. public Executive (String eName, String eAddress, String ePhone, String socSecNumber, double rate) super (eName, eAddress, ePhone, socSecNumber, rate); bonus = 0; // bonus has yet to be awarded } continue Copyright © 2012 Pearson Education, Inc.

//----------------------------------------------------------------- continue //----------------------------------------------------------------- // Awards the specified bonus to this executive. public void awardBonus (double execBonus) { bonus = execBonus; } // Computes and returns the pay for an executive, which is the // regular employee payment plus a one-time bonus. public double pay() double payment = super.pay() + bonus; bonus = 0; return payment; Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Hourly.java Author: Lewis/Loftus // // Represents an employee that gets paid by the hour. public class Hourly extends Employee { private int hoursWorked; //----------------------------------------------------------------- // Constructor: Sets up this hourly employee using the specified // information. public Hourly (String eName, String eAddress, String ePhone, String socSecNumber, double rate) super (eName, eAddress, ePhone, socSecNumber, rate); hoursWorked = 0; } continue Copyright © 2012 Pearson Education, Inc.

//----------------------------------------------------------------- continue //----------------------------------------------------------------- // Adds the specified number of hours to this employee's // accumulated hours. public void addHours (int moreHours) { hoursWorked += moreHours; } // Computes and returns the pay for this hourly employee. public double pay() double payment = payRate * hoursWorked; hoursWorked = 0; return payment; Copyright © 2012 Pearson Education, Inc.

//----------------------------------------------------------------- continue //----------------------------------------------------------------- // Returns information about this hourly employee as a string. public String toString() { String result = super.toString(); result += "\nCurrent hours: " + hoursWorked; return result; } Copyright © 2012 Pearson Education, Inc.

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism via Interfaces An interface name can be used as the type of an object reference variable Speaker current; The current reference can be used to point to any object of any class that implements the Speaker interface The version of speak that the following line invokes depends on the type of object that current is referencing current.speak(); © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism via Interfaces Suppose two classes, Philosopher and Dog, both implement the Speaker interface, providing distinct versions of the speak method In the following code, the first call to speak invokes one version and the second invokes another: Speaker guest = new Philospher(); guest.speak(); guest = new Dog(); © 2004 Pearson Addison-Wesley. All rights reserved

Summary Chapter 9 has focused 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 © 2004 Pearson Addison-Wesley. All rights reserved