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

Презентация:



Advertisements
Похожие презентации
2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
1/27 Chapter 9: Template Functions And Template Classes.
1 © Luxoft Training 2012 Inner and anonymous classes.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Optimizing BGP Scalability Implementing BGP Peer Groups.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
Unit II Constructor Cont… Destructor Default constructor.
© 2006 Cisco Systems, Inc. All rights reserved. HIPS v Configuring Rules Rule Basics.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Applying Route-Maps as BGP Filters.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
1 When And How To Make Interfaces Work For You Advanced OO Techniques Alex Herbstritt Principal Software Engineer, Progress Software Session 102.
© 2006 Cisco Systems, Inc. All rights reserved. MPLS v Complex MPLS VPNs Using Advanced VRF Import and Export Features.
© 2006 Cisco Systems, Inc. All rights reserved.BCMSN v Defining VLANs Correcting Common VLAN Configuration Errors.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
© 2006 Cisco Systems, Inc. All rights reserved. MPLS v MPLS VPN Implementation Configuring VRF Tables.
© 2006 Cisco Systems, Inc. All rights reserved. MPLS v MPLS VPN Implementation Configuring Small-Scale Routing Protocols Between PE and CE Routers.
Транксрипт:

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

2005 Pearson Education, Inc. All rights reserved Introduction Polymorphism – The same invocation can produce many forms of results

2005 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

2005 Pearson Education, Inc. All rights reserved. Polymorphism Different subclasses respond to the same message, possibly with different actions. +JuicePlease:Polite +JuicePlease:Rude+JuicePlease:InGerman

2005 Pearson Education, Inc. All rights reserved. Some Java Code Patron p1 = new Patron(); Patron p2 = new AmericanPatron(); Patron p3 = new BritishPatron(); Patron p4 = new GermanPatron(); p1.JuicePlease() // polite request p2. JuicePlease() // rude request p3. JuicePlease() // polite request p4. JuicePlease() // request in German (but polite)

2005 Pearson Education, Inc. All rights reserved. 6 Software Engineering Observation 10.2 Polymorphism promotes extensibility New object types that can respond to existing method calls can be incorporated into a system without requiring modification of the base system. Only client code that instantiates new objects must be modified to accommodate new types.

2005 Pearson Education, Inc. All rights reserved Demonstrating Polymorphic Behavior A superclass reference can be aimed at a subclass object – E.g., Patron p2 = new AmericanPatron(); – 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

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

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

2005 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 – 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

2005 Pearson Education, Inc. All rights reserved. 11 Software Engineering Observation 10.3 An abstract class declares common attributes and behaviors of the various classes in a class hierarchy. An abstract class typically contains one or more abstract methods that subclasses must override if the subclasses are to be concrete. The instance variables and concrete methods of an abstract class are subject to the normal rules of inheritance.

2005 Pearson Education, Inc. All rights reserved. 12 Common Programming Error 10.1 Attempting to instantiate an object of an abstract class is a compilation error.

2005 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 10.2 Failure to implement a superclasss abstract methods in a subclass is a compilation error unless the subclass is also declared abstract.

2005 Pearson Education, Inc. All rights reserved. 14 Fig | Employee hierarchy UML class diagram.

2005 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

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

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

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

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

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

2005 Pearson Education, Inc. All rights reserved. 21 Outline SalariedEmployee.java (2 of 2) Override earnings method so SalariedEmployee can be concrete Override toString method Call superclasss version of toString

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

2005 Pearson Education, Inc. All rights reserved. 23 Outline HourlyEmployee.java (2 of 2) Validate and set hours worked value Override earnings method so HourlyEmployee can be concrete Override toString method Call superclasss toString method

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

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

2005 Pearson Education, Inc. All rights reserved. 26 Outline CommissionEmployee.java (3 of 3) Override earnings method so CommissionEmployee can be concrete Override toString method Call superclasss toString method

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

2005 Pearson Education, Inc. All rights reserved. 28 Outline BasePlusCommission Employee.java (2 of 2) Override earnings method Call superclasss earnings method Override toString method Call superclasss toString method

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

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

2005 Pearson Education, Inc. All rights reserved. 31 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 objects class name

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

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

2005 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

2005 Pearson Education, Inc. All rights reserved. 35 Common Programming Error 10.3 Assigning a superclass variable to a subclass variable (without an explicit cast) is a compilation error.

2005 Pearson Education, Inc. All rights reserved. 36 Common Programming Error 10.4 When downcasting an object, a ClassCastException occurs, if at execution time the object does not have an is-a relationship with the type specified in the cast operator. An object can be cast only to its own type or to the type of one of its superclasses.

2005 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 classs name

2005 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

2005 Pearson Education, Inc. All rights reserved. 39 Common Programming Error 10.5 Attempting to declare a subclass of a final class is a compilation error.

2005 Pearson Education, Inc. All rights reserved. 40 In the Java, the vast majority of classes are not declared final. This enables inheritance and polymorphismthe fundamental capabilities of object-oriented programming. Software Engineering Observation 10.6