Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Object-Oriented Programming: Polymorphism PowerPoint Presentation
Download Presentation
Object-Oriented Programming: Polymorphism

Object-Oriented Programming: Polymorphism

112 Views Download Presentation
Download Presentation

Object-Oriented Programming: Polymorphism

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 12 • Object-Oriented Programming: • Polymorphism

  2. One Ring to rule them all,One Ring to find them,One Ring to bring them alland in the darkness bind them. J. R. R. Tolkien General propositions do notdecide concrete cases. Oliver Wendell Holmes

  3. A philosopher of imposing stature doesn’tthink in a vacuum. Even his most abstract ideas are, to some extent, conditioned by what is or isnot known in the time when he lives. Alfred North Whitehead Why art thou cast down, O my soul? Psalms 42:5

  4. OBJECTIVES In this chapter you will learn: • What polymorphism is. • To use overridden methods to effect polymorphism. • To distinguish between abstract and concrete classes. • To declare abstract methods to create abstract classes.

  5. OBJECTIVES • How polymorphism makes systems extensible and maintainable. • To determine an object’s type at execution time. • To declare and implement interfaces.

  6. 12.1  Introduction • 12.2  Polymorphic Video Game • 12.3  Demonstrating Polymorphic Behavior • 12.4  Abstract Classes and Methods • 12.5  Case Study: Payroll System Class Hierarchy Using Polymorphism • 12.6  NotOverridable Methods and NotInheritableClasses • 12.7  Case Study: Creating and Using Interfaces • 12.8  (Optional) Software Engineering Case Study: Incorporating Inheritance and Polymorphism into the ATM System

  7. 12.1  Introduction • Polymorphism enables us to “program in the general.” • New classes can be added to an inheritance hierarchy with little or no modification to the general portions of the program.

  8. 12.2  Polymorphic Video Game • Suppose we design a video game with objects of many types, inheriting from class SpaceObject • The screen manager makes the same method call, Draw, for each object. • Each derived class implements Draw in a manner appropriate to that class.

  9. 12.2  Polymorphic Video Game (Cont.) Software Engineering Observation 12.1 Software that invokes polymorphic behavior is independent of the object types to which messages are sent. New object types that can respond to existing method calls can be incorporated into a system without requiring modification

  10. To perform a derived class-specific operation, the program must downcast a base class reference. • Figure 12.1 shows three ways to use base class and derived class variables.. Outline PolymorphismTest.vb ( 1 of 3 ) CommissionEmployee object assigned to a CommissionEmployee variable. BasePlusCommission­Employee object assigned to a BasePlusCommissionEmployee variable. Fig. 12.1|Assigning base class and derived class references to base class and derived class variables.(Part 1 of 3.)

  11. Outline PolymorphismTest.vb ( 2 of 3 ) BasePlusCommission­Employee object assigned to a Commission­Employee variable. Calling ToString polymorphically. Fig. 12.1|Assigning base class and derived class references to base class and derived class variables.(Part 2 of 3.)

  12. Outline PolymorphismTest.vb ( 3 of 3 ) Fig. 12.1|Assigning base class and derived class references to base class and derived class variables.(Part 3 of 3.)

  13. 12.4  Abstract Classes and Methods • Abstract classes are incomplete classes which cannot be instantiated. • In the Shape hierarchy, we could derive concrete classes from abstract class TwoDimensionalShape. • A programmer can use an abstract base class as a parameter type. • These methods can be passed an object of any concrete class that inherits the base class.

  14. 12.4  Abstract Classes and Methods (Cont.) • Make a class abstract by declaring it with MustInherit. • Abstract methods have keyword MustOverride in their declarations: Public MustOverride Sub Draw() ' abstract method • A class that contains any abstract methods must be declared as an abstract class.

  15. 12.4   Abstract Classes and Methods (Cont.) Software Engineering Observation 12.2 An abstract class typically contains one or more abstract methods that derived classes must override if the derived classes are to be concrete. The instance variables and concrete methods of an abstract class are subject to the normal rules of inheritance.

  16. 12.4   Abstract Classes and Methods (Cont.) Common Programming Error 12.1 Attempting to instantiate an object of an abstractclass is a compilation error. Common Programming Error 12.2 Failure to implement a base class’s abstract methods in a derived class is a compilation errorunless the derived class is also declared MustInherit.

  17. 12.4  Abstract Classes and Methods (Cont.) • It is common to declare an iterator class that can represent all the objects in a collection, such as an array or a List. • An ArrayList of objects of class TwoDimensionalShape could contain objects from derived classes Square, Circle, Triangle and so on.

  18. 12.5  Case Study: Payroll System Class Hierarchy Using Polymorphism • We create an enhanced employee hierarchy to solve the following problem: A company wants to implement an application that performs its payroll calculations on a weekly basis. Salaried employees are paid a fixed weekly salary, hourly employees are paid by the hour and receive overtime pay (1.5 times the regular hourly salary), commission employees are paid a percentage of their sales, and other employees receive a base salary plus a percentage of their sales.

  19. 12.5  Case Study: Payroll System Class Hierarchy Using Polymorphism (Cont.) • The UML class diagram in Fig. 12.2 shows an inheritance hierarchy. • Note that abstract class name Employee is italicized. Fig. 12.2 | Employee hierarchy UML class diagram.

  20. 12.5  Case Study: Payroll System Class Hierarchy Using Polymorphism (Cont.) 12.5.1 Creating Abstract Base Class Employee • Class Employee will provide methods CalculateEarnings and ToString, and properties that manipulate instance variables. • We declare CalculateEarnings as MustOverride so that each derived class provides an appropriate implementation. • Each derived class of Employee overrides method ToString to include the employee’s type.

  21. 12.5  Case Study: Payroll System Class Hierarchy Using Polymorphism (Cont.) • The diagram in Fig.12.3 shows polymorphic variations of the Employee class’s methods. Fig. 12.3 | Polymorphic interface for the Employee hierarchy classes.

  22. Outline • Class Employee is shown in Fig. 12.4. Employee.vb ( 1 of 3 ) Using MustInherit makes the class abstract. Fig. 12.4|Employee abstract base class. (Part 1 of 3.)

  23. Outline Employee.vb ( 2 of 3 ) Fig. 12.4|Employee abstract base class. (Part 2 of 3.)

  24. Outline Employee.vb ( 3 of 3 ) Using MustOverride makes the method abstract. Fig. 12.4|Employee abstract base class. (Part 3 of 3.)

  25. Outline • Class SalariedEmployee (Fig. 12.5) is a concrete class which inherits an abstract class. SalariedEmployee.vb ( 1 of 2 ) Class SalariedEmployee inherits from abstract class Employee. Calling the Employee constructor. Fig. 12.5|SalariedEmployee class derived from classEmployee. (Part 1 of 2.)

  26. Outline SalariedEmployee.vb ( 2 of 2 ) Overriding Calculate­Earnings makes SalariedEmployee a concrete class. Outputting the employee’s type, information produced by Employee’s ToString method and the WeeklySalary property. Fig. 12.5|SalariedEmployee class derived from classEmployee. (Part 2 of 2.)

  27. Outline • Class HourlyEmployee (Fig. 12.6) also inherits class Employee. HourlyEmployee.vb ( 1 of 4 ) Class HourlyEmployee inherits from abstract class Employee. Calling the Employee constructor. Fig. 12.6|HourlyEmployee class derived from class Employee. (Part 1 of 4.)

  28. Outline HourlyEmployee.vb ( 2 of 4 ) Fig. 12.6|HourlyEmployee class derived from class Employee. (Part 2 of 4.)

  29. Outline HourlyEmployee.vb ( 3 of 4 ) Overriding Calculate­Earnings makes HourlyEmployee a concrete class. Fig. 12.6|HourlyEmployee class derived from class Employee. (Part 3 of 4.)

  30. Outline HourlyEmployee.vb ( 4 of 4 ) Outputting the employee’s type, information produced by Employee’s ToString method and more specific information. Fig. 12.6|HourlyEmployee class derived from class Employee. (Part 4 of 4.)

  31. Outline • Class CommissionEmployee (Fig. 12.7) is another extension of Employee. CommissionEmployee.vb ( 1 of 3 ) Class CommissionEmployee inherits from abstract class Employee. Calling the Employee constructor. Fig. 12.7|CommissionEmployee class derived from Employee. (Part 1 of 3.)

  32. Outline CommissionEmployee.vb ( 2 of 3 ) Fig. 12.7|CommissionEmployee class derived from Employee. (Part 2 of 3.)

  33. Outline CommissionEmployee.vb ( 3 of 3 ) Overriding Calculate­Earnings makes HourlyEmployee a concrete class. Outputting the employee’s type, information produced by Employee’s ToString method and more specific information. Fig. 12.7|CommissionEmployee class derived from Employee. (Part 3 of 3.)

  34. Outline • BasePlusCommissionEmployee (Fig. 12.8) inherits CommissionEmployee and therefore isan indirect derived class of Employee. BasePlusCommissionEmployee.vb ( 1 of 2 ) Class BasePlusCommissionEmployee inherits from class CommissionEmployee. Calling the Employee constructor. Fig. 12.8|BasePlusCommissionEmployee derived from CommissionEmployee. (Part 1 of 2.)

  35. Outline BasePlusCommissionEmployee.vb ( 2 of 2 ) Outputting the employee’s type, information produced by Employee’s ToString method and more specific information. Fig. 12.8|BasePlusCommissionEmployee derived from CommissionEmployee. (Part 2 of 2.)

  36. Outline • The program in Fig. 12.9 creates an object of each of the four concrete classes. PayrollSystemTest.vb ( 1 of 6 ) Creating objects of each of the four Employee derived classes. Fig. 12.9|Employee class hierarchy test program. (Part 1 of 6.)

  37. Outline PayrollSystemTest.vb ( 2 of 6 ) Storing Employee objects in an array. Fig. 12.9|Employee class hierarchy test program. (Part 2 of 6.)

  38. Outline PayrollSystemTest.vb ( 3 of 6 ) Fig. 12.9|Employee class hierarchy test program. (Part 3 of 6.)

  39. Outline PayrollSystemTest.vb ( 4 of 6 ) Outputting Employee object types polymorphically. Fig. 12.9|Employee class hierarchy test program. (Part 4 of 6.)

  40. Outline PayrollSystemTest.vb ( 5 of 6 ) Fig. 12.9|Employee class hierarchy test program. (Part 5 of 6.)

  41. Outline PayrollSystemTest.vb ( 6 of 6 ) Fig. 12.9|Employee class hierarchy test program. (Part 6 of 6.)

  42. 12.5  Case Study: Payroll System Class Hierarchy Using Polymorphism (Cont.) • The output polymorphically displays the earnings and types of differentEmployeeobjects. Using Expression TypeOf …Is to DetermineObject Type • TypeOf...Istests for a particular type.

  43. 12.5  Case Study: Payroll System Class Hierarchy Using Polymorphism (Cont.) Using TryCast to Downcast from a Base Class to a Derived Class Type • TryCastattempts to downcast a base class type to a derived class type. • If the object is not an instance of the derived class,TryCastreturnsNothing. • GetTypereturns aTypeobject which contains information about an object’s type.

  44. 12.5  Case Study: Payroll System Class Hierarchy Using Polymorphism (Cont.) Common Programming Error 12.3 Assigning a base class variable to a derived class variable (without an explicit cast) is a compilation error. Error-Prevention Tip 12.1 Before performing a downcast, use the Typeof...Is expression to ensure that the object is indeed an object of an appropriate derived class type. Then use DirectCast to downcast from the base class type to the derived class type.

  45. 12.6  NotOverridable Methods and NotInheritable Classes • A method that is declared NotOverridable cannot be overridden. • An inherited method can be declared NotOverridable in a derived class to prevent further inheritance. • A class that is declared NotInheritable cannot be a base class. Common Programming Error 12.4 Attempting to inherit a NotInheritable class is a compilation error.

  46. 12.7  Case Study: Creating and Using Interfaces • A Visual Basic interface describes a set of methods that can be called on an object. • An interface declaration begins with keyword Interface and can contain abstract methods and properties. • To use an interface, a concrete class must specify that it Implements the interface and must implement each method.

  47. 12.7  Case Study: Creating and Using Interfaces (Cont.) Common Programming Error 12.5 It is a compilation error to explicitly declare interface methods Public. Common Programming Error 12.6 In Visual Basic, an Interface should be declaredonly as Public or Friend; the declaration of an Interface as Private or Protected is only possible within another type.

  48. 12.7  Case Study: Creating and Using Interfaces (Cont.) Common Programming Error 12.7 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 MustInherit.

  49. 12.7  Case Study: Creating and Using Interfaces (Cont.) 12.7.1 Developing an IPayable Hierarchy • Interface IPayable contains method GetPaymentAmount, which returns a Decimal, and ToString. • Classes Invoice and Employee will implement interface IPayable. • A program will be able to invoke GetPaymentAmount on Invoices, Employees and any future objects.

  50. 12.7  Case Study: Creating and Using Interfaces (Cont.) Good Programming Practice 12.1 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 many unrelated classes.