SlideServe
Menu
Préférences
© SlideServe · v2026

Object-Oriented Programming Principles: Polymorphism, Encapsulation, Inheritance

lflavin avatar
44 diapositives · 509 Views · Jan 07, 2020

Understand the OOP trilogy of concepts: polymorphism, encapsulation, and inheritance. Learn how to implement these principles in programming using real-world examples. Dive into the world of object-oriented programming for efficient development.

Object-Oriented Programming Principles: Polymorphism, Encapsulation, Inheritance - slide 1
01 / 44Object-Oriented Programming Principles: Polymorphism, Encapsulation, Inheritance
1 / 17

Télécharger la présentation

Object-Oriented Programming Principles: Polymorphism, Encapsulation, Inheritance
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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