Chapter 10 Object-Oriented Programming: Polymorphism 383 Outline 10.1 (Bulletproof web design)
Chapter 10 Object-Oriented Programming: Polymorphism 383 Outline 10.1 Introduction 10.2 Derived-Class-Object to Base-Class-Object Conversion 10.3 Type Fields and switch Statements 10.4 Polymorphism Examples 10.5 Abstract Classes and Methods 10.6 Case Study: Inheriting Interface and Implementation 10.7 sealed Classes and Methods 10.8 Case Study: Payroll System Using Polymorphism 10.9 Case Study: Creating and Using Interfaces 10.10 Delegates 10.11 Operator Overloading Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 10.1 Introduction The previous chapter s object-oriented programming (OOP) discussion focussed on one of OOP s key component technologies, inheritance. In this chapter, we continue our study of OOP polymorphism. Both inheritance and polymorphism are crucial technologies in the development of complex software. Polymorphism enables us to write programs that handle a wide variety of related classes in a generic manner and facilitates adding new classes and capabilities to a system. With polymorphism, it is possible to design and implement systems that are easily extensible. Programs can process objects of all classes in a class hierarchy generically as objects of a common base class. Furthermore, new classes can be added with little or no modification to the generic portions of the program, as long as those classes are part of the inheritance hierarchy that the program processes generically. The only parts of a program that must be altered to accommodate new classes are those program components that require direct knowledge of the new classes that the programmer adds to the hierarchy. In this chapter, we demonstrate two substantial class hierarchies and manipulate objects from those hierarchies polymorphically. 10.2 Derived-Class-Object to Base-Class-Object Conversion Section 9.4 created a point-circle class hierarchy, in which class Circle inherited from class Point. The programs that manipulated objects of these classes always used Point references to refer to Point objects and Circle references to refer to Circle objects. In this section, we discuss the relationships between classes in a hierarchy that enable programs to assign derived-class objects to base-class references a fundamental part of programs that process objects polymorphically. This section also discusses explicit casting between types in a class hierarchy. An object of a derived class can be treated as an object of its base class. This enables various interesting manipulations. For example, a program can create an array of base-class
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.