440 likes | 486 Views
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
E N D
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 • Many objects may belong to the same class • User-defined, non-built-in type
The OOP Trilogy • Three important OO principles • Encapsulation • Inheritance • Polymorphism
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!
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!
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!
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
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
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()
Abstract Classes • Classes that are too general to create real objects • Used only as abstract base classes for concrete derived classes
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
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
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
Other Derived Classes • SalariedEmployee, HourlyEmployee • Similar to CommissionEmployee • override the Earnings and ToString methods
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
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?
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
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
Exercise • What if polymorphism is not used in the payroll example?
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
Summary • Polymorphism • promotes extensibility • requires late binding (virtual or abstract method) • abstract, sealed classes and methods