1 / 57

Object-Oriented Programming: Polymorphism

Object-Oriented Programming: Polymorphism. CISC6795: Spring 6795. Outline. 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

Faraday
Download Presentation

Object-Oriented Programming: Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Outline • 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 • 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 • 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 • 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 • 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

  8. Outline Polymorphically call basePlusCommissionEmployee’s toString method

  9. Outline • 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 • 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 • 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 • abstract superclass Employee • earnings is declared abstract • No implementation can be given for earnings in Employee class

  13. Outline Declare abstract class Employee Attributes common to all employees

  14. Outline

  15. Outline abstract method earnings has no implementation

  16. Outline Class SalariedEmployee extends class Employee Call superclass constructor

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

  18. Outline Class HourlyEmployee extends class Employee Call superclass constructor

  19. Outline Override earnings method so HourlyEmployee can be concrete Override toString method

  20. Outline

  21. Outline

  22. Outline

  23. Outline Class BasePlusCommissionEmployee extends class CommissionEmployee

  24. Outline Override toString method Call superclass’s toString method

  25. Outline

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

  27. If the currentEmployee variable points to a BasePlusCommissionEmployee object Outline Downcast Give BasePlusCommissionEmployees a 10% base salary bonus Polymorphically call earnings method Call getClass and getName methods to display each Employee subclass object’s class name

  28. Outline

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

  30. Downcasting • 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 ClassCastExceptionoccurs, if the object referenced does not have an is-arelationship with the type specified in cast operator • If unsure, use instanceof to check whether object is indeed an object of an appropriate subclass type

  31. Operator instanceof and Downcasting • 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

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

  33. Performance Tip • 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.

  34. Outline • 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

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

  36. Class Implement Interfaces • A class can implement multiple interfaces • Use a comma-separated list of interface names : publicclassClassNameextendsSuperclassNameimplementsFirstInterface,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.

  37. Developing a Payable Hierarchy • 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.

  38. Payable interface hierarchy UML diagram.

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

  40. Outline Class Invoice implements interface Payable

  41. Outline

  42. Outline Declare getPaymentAmount to fulfill contract with interface Payable

  43. Outline Class Employee implements interface Payable

  44. Outline

  45. Outline getPaymentAmount method is not implemented here

  46. Interface Implementation & Inheritance Hierarchy • 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.

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

  48. Outline Declare getPaymentAmount method instead of earnings method

  49. Software Engineering Observation • “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).

More Related