30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10 Object-Oriented <strong>Program</strong>ming: Polymorphism 389<br />

Alfred North Whitehead<br />

Outline<br />

10.1 Introduction<br />

10.2 Derived-Class-Object <strong>to</strong> Base-Class-Object Conversion<br />

10.3 Type Fields and Select Case Statements<br />

10.4 Polymorphism Examples<br />

10.5 Abstract Classes and Methods<br />

10.6 Case Study: Inheriting Interface and Implementation<br />

10.7 NotInheritable Classes and NotOverridable Methods<br />

10.8 Case Study: Payroll System Using Polymorphism<br />

10.9 Case Study: Creating and Using Interfaces<br />

10.10 Delegates<br />

Summary Terminology Self-Review Exercises Answers <strong>to</strong> Self-Review Exercises Exercises<br />

10.1 Introduction<br />

The previous chapter’s object-oriented programming (OOP) discussion focussed on one of its<br />

key component technologies, inheritance. In this chapter, we continue our study of OOP polymorphism.<br />

Both inheritance and polymorphism are crucial technologies in the development<br />

of complex software. Polymorphism enables us <strong>to</strong> write programs that handle a wide variety<br />

of related classes and facilitates adding new classes and capabilities <strong>to</strong> a system.<br />

Using polymorphism, it is possible <strong>to</strong> design and implement systems that are easily<br />

extensible. <strong>Program</strong>s can process objects of all classes in a class hierarchy generically as<br />

objects of a common base class. Furthermore, a new class can be added with little or no<br />

modification <strong>to</strong> the generic part of the program, as long as those new classes are part of the<br />

inheritance hierarchy that the program generically processes. The only parts of a program<br />

that must be altered <strong>to</strong> accommodate new classes are those program components that<br />

require direct knowledge of the new classes that the programmer adds <strong>to</strong> the hierarchy. In<br />

this chapter, we demonstrate two substantial class hierarchies and manipulate objects from<br />

those hierarchies polymorphically.<br />

10.2 Derived-Class-Object <strong>to</strong> Base-Class-Object Conversion<br />

Section 9.4 created a point-circle class hierarchy, in which class CCircle inherited from<br />

class CPoint. The programs that manipulated objects of these classes always used<br />

CPoint references <strong>to</strong> refer <strong>to</strong> CPoint objects and CCircle references <strong>to</strong> refer <strong>to</strong><br />

CCircle objects. In this section, we discuss the relationship between classes in a hierarchy<br />

that enables a program <strong>to</strong> assign derived-class objects <strong>to</strong> base-class references—a fundamental<br />

part of programs that process objects polymorphically. This section also<br />

discusses explicit casting between types in a class hierarchy.<br />

An object of a derived class can be treated as an object of its base class. This enables<br />

various interesting manipulations. For example, a program can create an array of base-class<br />

references that refer <strong>to</strong> objects of many derived-class types. This is allowed despite the fact

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!