Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC6795: Spring 6795 1 Object-Oriented Programming: Polymorphism.

Similar presentations


Presentation on theme: "CISC6795: Spring 6795 1 Object-Oriented Programming: Polymorphism."— Presentation transcript:

1 CISC6795: Spring 6795 1 Object-Oriented Programming: Polymorphism

2 Outline 2 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface

3 Is-a relationship & Polymorphic Behavior 3 A superclass reference can be aimed at a subclass object b.c. a subclass object is a superclass object as well Methods can be overriden When invoking a method from a superclass reference, the type of the actual referenced object determines which method is called Polymorphism: enables “programming in the general” Same invocation can produce “many forms” of results

4 Polymorphism 4 When invoking a method through a superclass reference, the correct subclass version of method is called, based on the type of the object being referenced by superclass reference Same method name and signature can cause different actions to occur, depending on the type of object on which the method is invoked Dynamic binding, also known as late binding Calls to overridden methods are resolved at execution time, based on the type of object referenced

5 Advantage of polymorphism: generality 5 Polymorphism enables programmers to deal in generalities Execution-time environment handle specifics Programmers command objects to behave in manners appropriate to those objects, without knowing the types of the objects (as long as the objects belong to the same inheritance hierarchy)

6 Advantage of polymorphism: extensibility 6 Extensibility: Software that invokes polymorphic behavior is independent of the object types to which messages are sent. Adding new classes to a system with minimal modifications New object types responding 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 Outline Typical reference assignments 7

8 Outline Polymorphically call basePlusCommissionEmployee ’s toString method 8

9 Outline 9 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface

10 Abstract Classes 10 Abstract classes: classes that are too general to create real objects Cannot be instantiated, doing so leads to a compilation error. Used only as abstract superclasses for concrete subclasses and to declare reference variables In inheritance hierarchies, abstract superclasses often occupy top few levels Abstract class declares common attributes and behaviors of various classes in a class hierarchy. Typically contains one or more abstract methods that subclasses must override for them to be concrete. Instance variables and concrete methods of an abstract class are subject to normal rules of inheritance.

11 Abstract Classes and Methods 11 Use keyword abstract to declare abstract class, or method Abstract classes normally contain one or more abstract methods All concrete subclasses must override all inherited abstract methods Concrete classes: classes that can be instantiated

12 Creating Abstract Superclass Employee 12 abstract superclass Employee earnings is declared abstract No implementation can be given for earnings in Employee class

13 13

14 Outline Declare abstract class Employee Attributes common to all employees 14

15 Outline 15

16 Outline 16 abstract method earnings has no implementation

17 Outline 17 Class SalariedEmployee extends class Employee Call superclass constructor

18 Outline 18 Override earnings method so SalariedEmployee can be concrete Call superclass’s version of toString

19 Outline 19 Class HourlyEmployee extends class Employee Call superclass constructor

20 Outline 20 Override earnings method so HourlyEmployee can be concrete Override toString method

21 Outline 21

22 Outline 22

23 Outline 23

24 Outline 24 Class BasePlusCommissionEmployee extends class CommissionEmployee

25 Outline 25 Override toString method Call superclass’s toString method

26 Outline 26

27 Outline Assigning subclass objects to supercalss variables Implicitly and polymorphically call toString

28 Outline If the currentEmployee variable points to a BasePlusCommissionEmployee object Downcast 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

29 Outline

30 30 Same results as when the employees were processed individually Base salary is increased by 10% Each employee’s type is displayed

31 Downcasting 31 Assigning a superclass variable to a subclass variable (without an explicit cast) is a compilation error. If at execution time the reference of a subclass object has been assigned to a superclass (direct or indirect) variable, one can downcast reference back to a reference of subclass type. An object can be cast only to its own type or to the type of one of its superclasses a ClassCastException occurs, if the object referenced does not have an is-a relationship with the type specified in cast operator If unsure, use instanceof to check whether object is indeed an object of an appropriate subclass type

32 Operator instanceof and Downcasting 32 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

33 final Methods and Classes 33 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 Attempting to declare a subclass of a final class is a compilation error. All methods in a final class are implicitly final

34 Performance Tip 34 The compiler can decide to inline a final method call and will do so for small, simple final methods. Inlining does not violate encapsulation or information hiding, but does improve performance because it eliminates the overhead of making a method call. In the Java API, the vast majority of classes are not declared final. This enables inheritance and polymorphism—the fundamental capabilities of object-oriented programming. However, in some cases, it is important to declare classes final —typically for security reasons.

35 Outline 35 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface

36 Creating and Using Interfaces 36 Interfaces, classes declared with keyword interface Contains only constants and abstract methods All fields are implicitly public, static and final All methods are implicitly public abstract methods 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

37 Class Implement Interfaces 37 A class can implement multiple interfaces Use a comma-separated list of interface names : public class ClassName extends SuperclassName implements FirstInterface, SecondInterface, … Declare each method in the interface using same signature or the class must be declared abstract Failing to implement any method of an interface in a concrete class that implements the interface results in a syntax error indicating that the class must be declared abstract.

38 Developing a Payable Hierarchy 38 Payable interface Contains method getPaymentAmount Is implemented by the Invoice and Employee classes When declaring a method in an interface, choose a method name that describes the method’s purpose in a general manner, because the method may be implemented by a broad range of unrelated classes.

39 Payable interface hierarchy UML diagram. 39

40 Outline 40 Declare interface Payable Declare getPaymentAmount method which is implicitly public and abstract

41 Outline Class Invoice implements interface Payable 41

42 Outline 42

43 Outline Declare getPaymentAmount to fulfill contract with interface Payable 43

44 Outline Class Employee implements interface Payable 44

45 Outline 45

46 Outline getPaymentAmount method is not implemented here 46

47 Interface Implementation & Inheritance Hierarchy 47 Objects of any subclasses of a class that implements an 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 its superclass implements that interface Inheritance and interfaces are similar in their implementation of the “is-a” relationship. An object of a class that implements an interface may be thought of as an object of that interface type An object of any subclasses of a class that implements an interface also can be thought of as an object of the interface type.

48 Outline Class SalariedEmployee extends class Employee (which implements interface Payable ) 48

49 Outline Declare getPaymentAmount method instead of earnings method 49

50 Software Engineering Observation 50 “is-a” relationship between superclasses and subclasses, and between interfaces and the classes that implement them, holds when passing an object to a method. When a method parameter receives a variable of a superclass or interface type, the method processes the object received as an argument polymorphically. Using a superclass reference, we can polymorphically invoke any method specified in the superclass declaration (and in class Object ). Using an interface reference, we can polymorphically invoke any method specified in the interface declaration (and in class Object ).

51 Outline Declare array of Payable variables Assigning references to Invoice objects to Payable variables Assigning references to SalariedEmployee objects to Payable variables 51

52 Outline Call toString and getPaymentAmount methods polymorphically

53 Outline 53 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface

54 Abstract class vs interface Both can be used to define a type that permits multiple implementations Differences Abstract classes can contain implementations for some methods, interface can not Must be a subclass of an abstract class, a class can be a subclass of a certain class, and implement any other interfaces Prefer interface to abstract classes Easy to retrofit existing classes to implement a new interface E.g., implement Comparable interface so that it can be sorted by Array class E.g., implement Serializable interface to save objects into file… E.g., Implement Runnable for multithread execution 54

55 Common interfaces of Java API 55

56 Common interfaces of Java API (2) 56

57 Summary 57 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface Lab 6: to be assigned


Download ppt "CISC6795: Spring 6795 1 Object-Oriented Programming: Polymorphism."

Similar presentations


Ads by Google