1 / 44

Polymorphism

Polymorphism. Ch 12. Object-Oriented Programming. Object Abstraction of a read-world item (e.g., car) Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) Class Blueprint for instantiating (creating) objects

lflavin
Download Presentation

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. Polymorphism Ch 12

  2. Object-Oriented Programming • Object • Abstraction of a read-world item (e.g., car) • Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) • Class • Blueprint for instantiating (creating) objects • Many objects may belong to the same class • User-defined, non-built-in type

  3. The OOP Trilogy • Three important OO principles • Encapsulation • Inheritance • Polymorphism

  4. Encapsulation • Attributes & behaviors encapsulated (wrapped) into objects • Information hiding • Implementation details hidden within objects • You can drive a car without knowing details of how engine, transmission really work • Modularization!

  5. Inheritance • Sub classes inherit attributes and behaviors of super class, then add more • A convertible is a car, and has some additional characteristics • Code reuse!

  6. Polymorphism • A behavior may have many (poly) forms (morph) in different sub classes • Cars with auto or manual transmissions work differently when accelerating. • Method implementing a behavior in super class may be overridden in a sub class • Extensibility!

  7. Payroll Example

  8. Polymorphism • Enables “programming in the general” • The same method call can produce “many forms” of results • depending on the type of object on which the method is invoked • Promotes extensibility • Method call via base class reference can invoke a method overridden in a sub class • Adding a new sub class does not require changing the method call

  9. Demonstrating Polymorphic Behavior

  10. Result

  11. is-a Relationship • A base class reference can be aimed at a derived class object • Derived class object is a base class object too • A BasePlusCommissionEmployee is a CommissionEmployee • Cannot treat a base class object as a derived class object • A CommissionEmployee may not be a BasePlusCommissionEmployee

  12. Which Method to Call • When invoking a method from a base class reference • the type of the actual referenced object, not the type of the reference, determines which method is called • commissionEmployee2.Earnings() calls • BasePlusCommissionEmployee.Earnings() • Not CommissionEmployee.Earnings()

  13. Abstract Classes • Classes that are too general to create real objects • Used only as abstract base classes for concrete derived classes

  14. Abstract Methods • Abstract classes normally contain one or more abstract methods • Abstract methods do not provide implementations • Concrete derived classes must override all inherited abstract methods • Constructors and static methods cannot be abstract

  15. sealed Methods and Classes • Opposite of abstract methods and classes • A sealed method can not be overridden in a derived class • A sealed class can not be a base class for a derived class

  16. Payroll Example

  17. Using Polymorphism • abstract base class Employee • Earnings() is declared abstract • Derived classes override Earnings() • An array of Employee variables will store references to derived class objects • Earnings method calls from these variables will call the appropriate versions

  18. abstract Base Class

  19. Concrete Derived Class

  20. Further Derived Class

  21. Other Derived Classes • SalariedEmployee, HourlyEmployee • Similar to CommissionEmployee • override the Earnings and ToString methods

  22. Test

  23. Operator is and Downcasting • isoperator • Determines if a particular object is of a certain type or a derived class of that type • GetType method • Return an object of class Type • Downcasting • Converts a base class reference to a derived class reference • May fail if object is not of the derived class

  24. Polymorphism Promotes Extensibility • If a new derived class is added • Earnings() is overridden • Can a call to Earnings() through a base class reference invoke this new version?

  25. Dynamic Binding • Also known as late binding • Calls to overridden methods are resolved at execution time, based on the type of object referenced • Indicated by virtual and override • abstract method is implicitly virtual and MUST be overridden in concrete sub class • Otherwise, static (early) binding • Method calls resolved at compiling time

  26. Is Polymorphism Free? • What do I pay for polymorphism? • Efficiency • Late binding is slower than early binding • What if I don't want polymorphism? • Don't declare a method as virtual or abstract • Can still redefine it in a derived class • Using keyword new, not override

  27. Exercise • What if polymorphism is not used in the payroll example?

  28. Tradeoffs In base class - virtual, abstract In sub class new override Binding Early, static Late, dynamic Polymorphism No Yes Extensibility Low High Efficiency High Low

  29. Summary • Polymorphism • promotes extensibility • requires late binding (virtual or abstract method) • abstract, sealed classes and methods

More Related