Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемАрина Брюхова
1 2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism
2 2005 Pearson Education, Inc. All rights reserved Introduction Polymorphism – The same invocation can produce many forms of results
3 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
4 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
5 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)
6 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.
7 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
8 2005 Pearson Education, Inc. All rights reserved. 8 Outline PolymorphismTest.java (1 of 2) Typical reference assignments
9 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
10 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
11 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.
12 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.
13 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.
14 2005 Pearson Education, Inc. All rights reserved. 14 Fig | Employee hierarchy UML class diagram.
15 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
16 2005 Pearson Education, Inc. All rights reserved. 16 Fig | Polymorphic interface for the Employee hierarchy classes.
17 2005 Pearson Education, Inc. All rights reserved. 17 Outline Employee.java (1 of 3) Declare abstract class Employee Attributes common to all employees
18 2005 Pearson Education, Inc. All rights reserved. 18 Outline Employee.java (2 of 3)
19 2005 Pearson Education, Inc. All rights reserved. 19 Outline Employee.java (3 of 3) abstract method earnings has no implementation
20 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
21 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
22 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
23 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
24 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
25 2005 Pearson Education, Inc. All rights reserved. 25 Outline CommissionEmployee.java (2 of 3) Validate and set the gross sales value
26 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
27 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
28 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
29 2005 Pearson Education, Inc. All rights reserved. 29 Outline PayrollSystemTest.java (1 of 5)
30 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
31 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
32 2005 Pearson Education, Inc. All rights reserved. 32 Outline PayrollSystemTest.java (4 of 5)
33 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
34 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
35 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.
36 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.
37 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
38 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
39 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.
40 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
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.