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 9 Object-Oriented <strong>Program</strong>ming: Inheritance 373<br />

The first class that we use in our case study is class CPoint (Fig. 9.4). We declared<br />

CPoint’s instance variables as Private. Class CPoint also contains properties X and<br />

Y for accessing mX and mY and method ToString (which CPoint overrides from class<br />

Object) for obtaining a String representation of the x-y coordinate pair.<br />

We also created class CCircle4 (Fig. 9.12), which inherits from class CPoint.<br />

Class CCircle4 contains the CPoint functionality, in addition <strong>to</strong> providing property<br />

Radius, which ensures that the mRadius member variable cannot hold a negative value,<br />

and methods Diameter, Circumference, Area and ToString. Recall that method<br />

Area was declared Overridable (line 53). As we discussed in Section 9.4, this keyword<br />

enables derived classes <strong>to</strong> override a base-class method. Derived classes of class<br />

CCircle4 (such as class CCylinder, which we introduce momentarily) can override<br />

these methods and provide specific implementations. A circle has an area that is calculated<br />

by the equation<br />

πr 2<br />

in which r represents the circle’s radius. <strong>How</strong>ever, a cylinder has a surface area that is calculated<br />

by a different equation:<br />

(2πr 2 ) + (2πrh)<br />

in which r represents the cylinder’s radius and h represents the cylinder’s height. Therefore,<br />

class CCylinder must override method Area <strong>to</strong> include this calculation, so we declared<br />

class CCircle4’s method Area as Overridable.<br />

Figure 9.14 presents class CCylinder, which inherits from class CCircle4 (line<br />

5). Class CCylinder’s Public services include the inherited CCircle4 methods<br />

Diameter, Circumference, Area and ToString; the inherited CCircle4 property<br />

Radius; the indirectly inherited CPoint properties X and Y; the CCylinder construc<strong>to</strong>r,<br />

property Height and method Volume. Method Area (lines 43–45) overrides<br />

method Area of class CCircle4. Note that, if class CCylinder were <strong>to</strong> attempt <strong>to</strong><br />

override CCircle4’s methods Diameter and Circumference, syntax errors would<br />

occur, because class CCircle4 did not declare these methods Overridable. Method<br />

ToString (lines 53–55) overrides method ToString of class CCircle4 <strong>to</strong> obtain a<br />

String representation for the cylinder. Class CCylinder also includes method<br />

Volume (lines 48–50) <strong>to</strong> calculate the cylinder’s volume. Because we do not declare<br />

method Volume as Overridable, no derived class of class CCylinder can override<br />

this method.<br />

1 ' Fig. 9.14: Cylinder.vb<br />

2 ' CCylinder class inherits from class CCircle4.<br />

3<br />

4 Public Class CCylinder<br />

5 Inherits CCircle4<br />

6<br />

7 Protected mHeight As Double<br />

Fig. 9.14 CCylinder class inherits from class CCircle4 and Overrides<br />

method Area (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!