 1992-2007 Pearson Education, Inc. All rights reserved. 1 10 Object-Oriented Programming: Polymorphism.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Object-Oriented Programming: Polymorphism
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Java SE 8 for Programmers, Third Edition Advanced Java Programming.
Object Oriented Programming: Inheritance Chapter 9.
Object Oriented Programming using Java - Polymorphism
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
(C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
 2009 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Object Oriented Programming: Inheritance Chapter 9.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Object-Oriented Programming: Polymorphism
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Interface
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Based on slides from Deitel & Associates, Inc.
Advanced Java Topics Chapter 9
Abstract Classes and Interfaces
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
Presentation transcript:

 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism

 Pearson Education, Inc. All rights reserved. 2 OBJECTIVES In this chapter you will learn:  The concept of polymorphism.  To use overridden methods to effect polymorphism.  To distinguish between abstract and concrete classes.  To declare abstract methods to create abstract classes.  How polymorphism makes systems extensible and maintainable.  To determine an object's type at execution time.  To declare and implement interfaces.

 Pearson Education, Inc. All rights reserved Introduction Polymorphism – Enables “ programming in the general ” – The same invocation can produce “ many forms ” of results Interfaces – Implemented by classes to assign common functionality to possibly unrelated classes

 Pearson Education, Inc. All rights reserved Polymorphism Examples Polymorphism – When a program invokes a method through a superclass variable, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable – The same method name and signature can cause different actions to occur, depending on the type of object on which the method is invoked – Facilitates adding new classes to a system with minimal modifications to the system ’ s code

 Pearson Education, Inc. All rights reserved Demonstrating Polymorphic Behavior A superclass reference can be aimed at a subclass object – This is possible because a subclass object is a superclass object as well – When invoking a method from that reference, the type of the actual referenced object, not the type of the reference, determines which method is called A subclass reference can be aimed at a superclass object only if the object is downcasted

 Pearson Education, Inc. All rights reserved. 6 Outline PolymorphismTest.java (1 of 2) Typical reference assignments

 Pearson Education, Inc. All rights reserved. 7 Outline PolymorphismTest.java (2 of 2) Assign a reference to a basePlusCommissionEmployee object to a CommissionEmployee3 variable Polymorphically call basePlusCommissionEmployee ’s toString method

 Pearson Education, Inc. All rights reserved Abstract Classes and Methods Abstract classes – Classes that are too general to create real objects – Used only as abstract superclasses for concrete subclasses and to declare reference variables – Many inheritance hierarchies have abstract superclasses occupying the top few levels – Keyword abstract Use to declare a class abstract Also use to declare a method abstract – Abstract classes normally contain one or more abstract methods – All concrete subclasses must override all inherited abstract methods

 Pearson Education, Inc. All rights reserved Abstract Classes and Methods (Cont.) Iterator class – Traverses all the objects in a collection, such as an array – Often used in polymorphic programming to traverse a collection that contains references to objects from various levels of a hierarchy

 Pearson Education, Inc. All rights reserved. 10 Common Programming Error 10.2 Failure to implement a superclass ’ s abstract methods in a subclass is a compilation error unless the subclass is also declared abstract.

 Pearson Education, Inc. All rights reserved. 11 Fig | Employee hierarchy UML class diagram.

 Pearson Education, Inc. All rights reserved Creating Abstract Superclass Employee abstract superclass Employee – earnings is declared abstract No implementation can be given for earnings in the Employee abstract class – An array of Employee variables will store references to subclass objects earnings method calls from these variables will call the appropriate version of the earnings method

 Pearson Education, Inc. All rights reserved. 13 Fig | Polymorphic interface for the Employee hierarchy classes.

 Pearson Education, Inc. All rights reserved. 14 Outline Employee.java (1 of 3) Declare abstract class Employee Attributes common to all employees

 Pearson Education, Inc. All rights reserved. 15 Outline Employee.java (2 of 3)

 Pearson Education, Inc. All rights reserved. 16 Outline Employee.java (3 of 3) abstract method earnings has no implementation

 Pearson Education, Inc. All rights reserved. 17 Outline SalariedEmployee.java (1 of 2) Class SalariedEmployee extends class Employee Call superclass constructor Validate and set weekly salary value Call setWeeklySalary method

 Pearson Education, Inc. All rights reserved. 18 Outline SalariedEmployee.java (2 of 2) Override earnings method so SalariedEmployee can be concrete Override toString method Call superclass’s version of toString

 Pearson Education, Inc. All rights reserved. 19 Outline HourlyEmployee.java (1 of 2) Class HourlyEmployee extends class Employee Call superclass constructor Validate and set hourly wage value

 Pearson Education, Inc. All rights reserved. 20 Outline HourlyEmployee.java (2 of 2) Validate and set hours worked value Override earnings method so HourlyEmployee can be concrete Override toString method Call superclass’s toString method

 Pearson Education, Inc. All rights reserved. 21 Outline CommissionEmployee.java (1 of 3) Class CommissionEmployee extends class Employee Call superclass constructor Validate and set commission rate value

 Pearson Education, Inc. All rights reserved. 22 Outline CommissionEmployee.java (2 of 3) Validate and set the gross sales value

 Pearson Education, Inc. All rights reserved. 23 Outline CommissionEmployee.java (3 of 3) Override earnings method so CommissionEmployee can be concrete Override toString method Call superclass’s toString method

 Pearson Education, Inc. All rights reserved. 24 Outline BasePlusCommission Employee.java (1 of 2) Class BasePlusCommissionEmployee extends class CommissionEmployee Call superclass constructor Validate and set base salary value

 Pearson Education, Inc. All rights reserved. 25 Outline BasePlusCommission Employee.java (2 of 2) Override earnings method Call superclass’s earnings method Override toString method Call superclass’s toString method

 Pearson Education, Inc. All rights reserved. 26 Outline PayrollSystemTest.java (1 of 5)

 Pearson Education, Inc. All rights reserved. 27 Outline PayrollSystemTest.java (2 of 5) Assigning subclass objects to supercalss variables Implicitly and polymorphically call toString

 Pearson Education, Inc. All rights reserved. 28 Outline PayrollSystemTest.java (3 of 5) If the currentEmployee variable points to a BasePlusCommissionEmployee object Downcast currentEmployee to a BasePlusCommissionEmployee reference Give BasePlusCommissionEmployee s a 10% base salary bonus Polymorphically call earnings method Call getClass and getName methods to display each Employee subclass object’s class name

 Pearson Education, Inc. All rights reserved. 29 Outline PayrollSystemTest.java (4 of 5)

 Pearson Education, Inc. All rights reserved. 30 Outline PayrollSystemTest.java (5 of 5) Same results as when the employees were processed individually Base salary is increased by 10% Each employee’s type is displayed

 Pearson Education, Inc. All rights reserved Demonstrating Polymorphic Processing, Operator instanceof and Downcasting Dynamic binding – Also known as late binding – Calls to overridden methods are resolved at execution time, based on the type of object referenced instanceof operator – Determines whether an object is an instance of a certain type

 Pearson Education, Inc. All rights reserved Demonstrating Polymorphic Processing, Operator instanceof and Downcasting (Cont.) Downcasting – Convert a reference to a superclass to a reference to a subclass – Allowed only if the object has an is-a relationship with the subclass getClass method – Inherited from Object – Returns an object of type Class getName method of class Class – Returns the class ’ s name

 Pearson Education, Inc. All rights reserved Summary of the Allowed Assignments Between Superclass and Subclass Variables Superclass and subclass assignment rules – Assigning a superclass reference to a superclass variable is straightforward – Assigning a subclass reference to a subclass variable is straightforward – Assigning a subclass reference to a superclass variable is safe because of the is-a relationship Referring to subclass-only members through superclass variables is a compilation error – Assigning a superclass reference to a subclass variable is a compilation error Downcasting can get around this error

 Pearson Education, Inc. All rights reserved final Methods and Classes final methods – Cannot be overridden in a subclass – private and static methods are implicitly final – final methods are resolved at compile time, this is known as static binding Compilers can optimize by inlining the code final classes – Cannot be extended by a subclass – All methods in a final class are implicitly final

 Pearson Education, Inc. All rights reserved Case Study: Creating and Using Interfaces Interfaces – Keyword interface – Contains only constants and abstract methods All fields are implicitly public, static and final All methods are implicitly public abstract methods – Classes can implement interfaces The class must declare each method in the interface using the same signature or the class must be declared abstract – Typically used when disparate classes need to share common methods and constants – Normally declared in their own files with the same names as the interfaces and with the.java file-name extension

 Pearson Education, Inc. All rights reserved. 36 Good Programming Practice 10.1 According to Chapter 9 of the Java Language Specification, it is proper style to declare an interface ’ s methods without keywords public and abstract because they are redundant in interface method declarations. Similarly, constants should be declared without keywords public, static and final because they, too, are redundant.

 Pearson Education, Inc. All rights reserved Developing a Payable Hierarchy Payable interface – Contains method getPaymentAmount – Is implemented by the Invoice and Employee classes UML representation of interfaces – Interfaces are distinguished from classes by placing the word “ interface ” in guillemets ( « and » ) above the interface name – The relationship between a class and an interface is known as realization A class “ realizes ” the methods of an interface

 Pearson Education, Inc. All rights reserved. 38 Fig | Payable interface hierarchy UML class diagram.

 Pearson Education, Inc. All rights reserved. 39 Outline Payable.java Declare interface Payable Declare getPaymentAmount method which is implicitly public and abstract

 Pearson Education, Inc. All rights reserved. 40 Outline Invoice.java (1 of 3) Class Invoice implements interface Payable

 Pearson Education, Inc. All rights reserved. 41 Outline Invoice.java (2 of 3)

 Pearson Education, Inc. All rights reserved. 42 Outline Invoice.java (3 of 3) Declare getPaymentAmount to fulfill contract with interface Payable

 Pearson Education, Inc. All rights reserved Creating Class Invoice A class can implement as many interfaces as it needs – Use a comma-separated list of interface names after keyword implements Example: public class ClassName extends SuperclassName implements FirstInterface, SecondInterface, …

 Pearson Education, Inc. All rights reserved. 44 Outline Employee.java (1 of 3) Class Employee implements interface Payable

 Pearson Education, Inc. All rights reserved. 45 Outline Employee.java (2 of 3)

 Pearson Education, Inc. All rights reserved. 46 Outline Employee.java (3 of 3) getPaymentAmount method is not implemented here

 Pearson Education, Inc. All rights reserved Modifying Class SalariedEmployee for Use in the Payable Hierarchy Objects of any subclasses of the class that implements the interface can also be thought of as objects of the interface – A reference to a subclass object can be assigned to an interface variable if the superclass implements that interface

 Pearson Education, Inc. All rights reserved. 48 Outline SalariedEmployee.java (1 of 2) Class SalariedEmployee extends class Employee (which implements interface Payable )

 Pearson Education, Inc. All rights reserved. 49 Outline SalariedEmployee.java (2 of 2) Declare getPaymentAmount method instead of earnings method

 Pearson Education, Inc. All rights reserved. 50 Outline PayableInterface Test.java (1 of 2) Declare array of Payable variables Assigning references to Invoice objects to Payable variables Assigning references to SalariedEmployee objects to Payable variables

 Pearson Education, Inc. All rights reserved. 51 Outline PayableInterface Test.java (2 of 2) Call toString and getPaymentAmount methods polymorphically

 Pearson Education, Inc. All rights reserved Declaring Constants with Interfaces Interfaces can be used to declare constants used in many class declarations – These constants are implicitly public, static and final – Using a static import declaration allows clients to use these constants with just their names

 Pearson Education, Inc. All rights reserved. 53 Fig | Common interfaces of the Java API. (Part 1 of 2)

 Pearson Education, Inc. All rights reserved. 54 Fig | Common interfaces of the Java API. (Part 2 of 2)