Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.

Similar presentations


Presentation on theme: " 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived."— Presentation transcript:

1  2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived Classes 9.3 Protected and Friend Members 9.4 Relationship between Base Classes and Derived Classes 9.5 Constructors and Finalizers in Derived Classes 9.6 Implicit Derived-Class-Object to Base-Class-Object Conversion 9.7 Software Engineering with Inheritance 9.8 Composition vs. Inheritance 9.9 Case Study: Point, Circle, Cylinder 9.10 Visual Inheritance

2  2002 Prentice Hall. All rights reserved. 2 9.1 Introduction Fig. 9.1Some simple inheritance examples.

3  2002 Prentice Hall. All rights reserved. 3 9.2 Base Classes and Derived Classes Fig. 9.2Inheritance hierarchy for university CCommunityMembers. CommunityMemeber EmployeeStudentAlumnus FacultyStaff AdministratorTeacher

4  2002 Prentice Hall. All rights reserved. 4 9.3 Protected and Friend Members Fig. 9.3Portion of a CShape class hierarchy. CShape CTwoDimensionalShapeCThreeDimensionalShape CSphereCCubeCCylinderCTriangleCSquareCCircle

5  2002 Prentice Hall. All rights reserved. 5 9.4 Relationship Between Base Classes and Derived Classes Structural Inheritance –Base Class Base-class must be declared as overridable if that method is to be overridden in the derived class The base-class should be able to change its implementation freely –Derived Class Derived-class object can assign an illegal value to the Protected data, hence leaving the object in an inconsistent state Derived-classes should depend only on the base-classes services (non private methods and properties)

6  2002 Prentice Hall. All rights reserved. Outline 6 CPoint.vb 1 ' Fig. 9.4: Point.vb 2 ' CPoint class represents an x-y coordinate pair. 3 4 Public Class CPoint 5 ' implicitly Inherits Object 6 7 ' point coordinate 8 Private mX, mY As Integer 9 10 ' default constructor 11 Public Sub New() 12 13 ' implicit call to Object constructor occurs here 14 X = 0 15 Y = 0 16 End Sub ' New 17 18 ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) 21 22 ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 End Sub ' New 26 27 ' property X 28 Public Property X() As Integer 29 30 Get 31 Return mX 32 End Get 33 Two CPoint Constructors Property of X Declares integer variables mX and mY as Private

7  2002 Prentice Hall. All rights reserved. Outline 7 CPoint.vb 34 Set(ByVal xValue As Integer) 35 mX = xValue ' no need for validation 36 End Set 37 38 End Property ' X 39 40 ' property Y 41 Public Property Y() As Integer 42 43 Get 44 Return mY 45 End Get 46 47 Set(ByVal yValue As Integer) 48 mY = yValue ' no need for validation 49 End Set 50 51 End Property ' Y 52 53 ' return String representation of CPoint 54 Public Overrides Function ToString() As String 55 Return "[" & mX & ", " & mY & "]" 56 End Function ' ToString 57 58 End Class ' CPoint Method ToString contains keyword Overrides Property of Y

8  2002 Prentice Hall. All rights reserved. Outline 8 PointTest.vb 1 ' Fig. 9.5: PointTest.vb 2 ' Testing class CPoint. 3 4 Imports System.Windows.Forms 5 6 Module modPointTest 7 8 Sub Main() 9 Dim point As CPoint 10 Dim output As String 11 12 point = New CPoint(72, 115) ' instantiate CPoint object 13 14 ' display point coordinates via X and Y properties 15 output = "X coordinate is " & point.X & _ 16 vbCrLf & "Y coordinate is " & point.Y 17 18 point.X = 10 ' set x-coordinate via X property 19 point.Y = 10 ' set y-coordinate via Y property 20 21 ' display new point value 22 output &= vbCrLf & vbCrLf & _ 23 "The new location of point is " & point.ToString() 24 25 MessageBox.Show(output, "Demonstrating Class Point") 26 End Sub ' Main 27 28 End Module ' modPointTest Instantiates an object of class CPoint Sets X and Y coordinates Retrieves values X and Y and appends values to String Output Changes values of X and Y Outputs String representation of X and Y

9  2002 Prentice Hall. All rights reserved. Outline 9 Circle.vb 1 ' Fig. 9.6: Circle.vb 2 ' CCircle class contains x-y coordinate pair and radius. 3 4 Public Class CCircle 5 6 ' coordinate of center of CCircle 7 Private mX, mY As Integer 8 Private mRadius As Double ' CCircle's radius 9 10 ' default constructor 11 Public Sub New() 12 13 ' implicit call to Object constructor occurs here 14 X = 0 15 Y = 0 16 Radius = 0 17 End Sub ' New 18 19 ' constructor 20 Public Sub New(ByVal xValue As Integer, _ 21 ByVal yValue As Integer, ByVal radiusValue As Double) 22 23 ' implicit call to Object constructor occurs here 24 X = xValue 25 Y = yValue 26 Radius = radiusValue 27 End Sub ' New 28 29 ' property X 30 Public Property X() As Integer 31 32 Get 33 Return mX 34 End Get 35 Two CCircle constructors of X, Y coordinates and Radius Property of X Declared integer variables mX and mY as Private

10  2002 Prentice Hall. All rights reserved. Outline 10 Circle.vb 36 Set(ByVal xValue As Integer) 37 mX = xValue ' no need for validation 38 End Set 39 40 End Property ' X 41 42 ' property Y 43 Public Property Y() As Integer 44 45 Get 46 Return mY 47 End Get 48 49 Set(ByVal yValue As Integer) 50 mY = yValue ' no need for validation 51 End Set 52 53 End Property ' Y 54 55 ' property Radius 56 Public Property Radius() As Double 57 58 Get 59 Return mRadius 60 End Get 61 62 Set(ByVal radiusValue As Double) 63 64 If radiusValue > 0 65 mRadius = radiusValue 66 End If 67 68 End Set 69 70 End Property ' Radius Property of Y Property of Radius Ensures the member mRadius cannot be assigned a negative value

11  2002 Prentice Hall. All rights reserved. Outline 11 Circle.vb 71 72 ' calculate CCircle diameter 73 Public Function Diameter() As Double 74 Return mRadius * 2 75 End Function ' Diameter 76 77 ' calculate CCircle circumference 78 Public Function Circumference() As Double 79 Return Math.PI * Diameter() 80 End Function ' Circumference 81 82 ' calculate CCircle area 83 Public Function Area() As Double 84 Return Math.PI * mRadius ^ 2 85 End Function ' Area 86 87 ' return String representation of CCircle 88 Public Overrides Function ToString() As String 89 Return "Center = " & "[" & mX & ", " & mY & "]" & _ 90 "; Radius = " & mRadius 91 End Function ' ToString 92 93 End Class ' CCircle Methods to calculate Diameter, Circumference, Area, and ToString representation

12  2002 Prentice Hall. All rights reserved. Outline 12 InheritanceTest. vb 1 ' Fig. 9.7: CircleTest.vb 2 ' Testing class CCircle. 3 4 Imports System.Windows.Forms 5 6 Module modCircleTest 7 8 Sub Main() 9 Dim circle As CCircle 10 Dim output As String 11 12 circle = New CCircle(37, 43, 2.5) ' instantiate CCircle 13 14 ' get CCircle's initial x-y coordinates and radius 15 output = "X coordinate is " & circle.X & vbCrLf & _ 16 "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _ 17 circle.Radius 18 19 ' set CCircle's x-y coordinates and radius to new values 20 circle.X = 2 21 circle.Y = 2 22 circle.Radius = 4.25 23 24 ' display CCircle's String representation 25 output &= vbCrLf & vbCrLf & _ 26 "The new location and radius of circle are " & _ 27 vbCrLf & circle.ToString() & vbCrLf 28 29 ' display CCircle's diameter 30 output &= "Diameter is " & _ 31 String.Format("{0:F}", circle.Diameter()) & vbCrLf 32 Declares an object of class CCircle Retrieves then concatenate values X, Y and Radius to String output Resets values X, Y and Radius by using its properties Out puts Diameter

13  2002 Prentice Hall. All rights reserved. Outline 13 InheritanceTest. vb 33 ' display CCircle's circumference 34 output &= "Circumference is " & _ 35 String.Format("{0:F}", circle.Circumference()) & vbCrLf 36 37 ' display CCircle's area 38 output &= "Area is " & String.Format("{0:F}", circle.Area()) 39 40 MessageBox.Show(output, "Demonstrating Class CCircle") 41 End Sub ' Main 42 43 End Module ' modCircleTest Outputs circumference and area

14  2002 Prentice Hall. All rights reserved. Outline 14 Circle2.vb 1 ' Fig. 9.8: Circle2.vb 2 ' CCircle2 class that inherits from class CPoint. 3 4 Public Class CCircle2 5 Inherits CPoint ' CCircle2 Inherits from class CPoint 6 7 Private mRadius As Double ' CCircle2's radius 8 9 ' default constructor 10 Public Sub New() 11 12 ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New 15 16 ' constructor 17 Public Sub New(ByVal xValue As Integer, _ 18 ByVal yValue As Integer, ByVal radiusValue As Double) 19 20 ' implicit call to CPoint constructor occurs here 21 mX = xValue 22 mY = yValue 23 Radius = radiusValue 24 End Sub ' New 25 26 ' property Radius 27 Public Property Radius() As Double 28 29 Get 30 Return mRadius 31 End Get 32 The keyword Inherits indicates inheritance CCircle2 inherits all members of class CPoint except constructors Generates first syntax error

15  2002 Prentice Hall. All rights reserved. Outline 15 Circle.vb 33 Set ( ByVal radiusValue As Double ) 34 35 If radiusValue > 0 36 mRadius = radiusValue 37 End If 38 39 End Set 40 41 End Property ' Radius 42 43 ' calculate CCircle2 diameter 44 Public Function Diameter() As Double 45 Return mRadius * 2 46 End Function ' Diameter 47 48 ' calculate CCircle2 circumference 49 Public Function Circumference() As Double 50 Return Math. PI * Diameter() 51 End Function ' Circumference 52 53 ' calculate CCircle2 area 54 Public Function Area() As Double 55 Return Math. PI * mRadius ^ 2 56 End Function ' Area 57 58 ' return String representation of CCircle2 59 Public Overrides Function ToString() As String 60 Return "Center = " & "[" & mX & ", " & mY & "]" & _ 61 "; Radius = " & mRadius 62 End Function ' ToString 63 64 End Class ' CCircle2 Generates second syntax error Ensures that variable mRadius cannot be assigned a negative value

16  2002 Prentice Hall. All rights reserved. Outline 16 1 ' Fig. 9.9: Point2.vb 2 ' CPoint2 class contains an x-y coordinate pair as Protected data. 3 4 Public Class CPoint2 5 ' implicitly Inherits Object 6 7 ' point coordinate 8 Protected mX, mY As Integer 9 10 ' default constructor 11 Public Sub New() 12 13 ' implicit call to Object constructor occurs here 14 X = 0 15 Y = 0 16 End Sub ' New 17 18 ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) 21 22 ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 End Sub ' New 26 27 ' property X 28 Public Property X() As Integer 29 30 Get 31 Return mX 32 End Get 33 Class CPoint2 modifies class CPoint to declare variables mX and mY as Protected

17  2002 Prentice Hall. All rights reserved. Outline 17 34 Set(ByVal xValue As Integer) 35 mX = xValue ' no need for validation 36 End Set 37 38 End Property ' X 39 40 ' property Y 41 Public Property Y() As Integer 42 43 Get 44 Return mY 45 End Get 46 47 Set(ByVal yValue As Integer) 48 mY = yValue ' no need for validation 49 End Set 50 51 End Property ' Y 52 53 ' return String representation of CPoint2 54 Public Overrides Function ToString() As String 55 Return "[" & mX & ", " & mY & "]" 56 End Function ' ToString 57 58 End Class ' CPoint2

18  2002 Prentice Hall. All rights reserved. Outline 18 Circle3.vb 1 ' Fig. 9.10: Circle3.vb 2 ' CCircle3 class that inherits from class CPoint2. 3 4 Public Class CCircle3 5 Inherits CPoint2 ' CCircle3 Inherits from class CPoint2 6 7 Private m Radius As Double ' CCircle3's radius 8 9 ' default constructor 10 Public Sub New () 11 12 ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New 15 16 ' constructor 17 Public Sub New ( ByVal xValue As Integer, _ 18 ByVal yValue As Integer, ByVal radiusValue As Double ) 19 20 ' implicit call to CPoint2 constructor occurs here 21 mX = xValue 22 mY = yValue 23 Radius = radiusValue 24 End Sub ' New 25 26 ' property Radius 27 Public Property Radius() As Double 28 29 Get 30 Return mRadius 31 End Get 32 33 Set ( ByVal radiusValue As Double ) 34 Class CCircle3 inherits CPoint2

19  2002 Prentice Hall. All rights reserved. Outline 19 CCircle.vb 35 If radiusValue > 0 36 mRadius = radiusValue 37 End If 38 39 End Set 40 41 End Property ' Radius 42 43 ' calculate CCircle3 diameter 44 Public Function Diameter() As Double 45 Return mRadius * 2 46 End Function ' Diameter 47 48 ' calculate CCircle3 circumference 49 Public Function Circumference() As Double 50 Return Math.PI * Diameter() 51 End Function ' Circumference 52 53 ' calculate CCircle3 area 54 Public Overridable Function Area() As Double 55 Return Math.PI * mRadius ^ 2 56 End Function ' Area 57 58 ' return String representation of CCircle3 59 Public Overrides Function ToString() As String 60 Return "Center = " & "[" & mX & ", " & mY & "]" & _ 61 "; Radius = " & mRadius 62 End Function ' ToString 63 64 End Class ' CCircle3

20  2002 Prentice Hall. All rights reserved. Outline 20 CCircle.vb 1 ' Fig. 9.11: CircleTest3.vb 2 ' Testing class CCircle3. 3 4 Imports System.Windows.Forms 5 6 Module modCircleTest3 7 8 Sub Main() 9 Dim circle As CCircle3 10 Dim output As String 11 12 circle = New CCircle3( 37, 43, 2.5 ) ' instantiate CCircle3 13 14 ' get CCircle3's initial x-y coordinates and radius 15 output = "X coordinate is " & circle.X & vbCrLf & _ 16 "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _ 17 circle.Radius 18 19 ' set CCircle3's x-y coordinates and radius to new values 20 circle.X = 2 21 circle.Y = 2 22 circle.Radius = 4.25 23 24 ' display CCircle3's String representation 25 output &= vbCrLf & vbCrLf & _ 26 "The new location and radius of circle are " & _ 27 vbCrLf & circle.ToString() & vbCrLf 28 29 ' display CCircle3's diameter 30 output &= "Diameter is " & _ 31 String.Format( "{0:F}", circle.Diameter()) & vbCrLf 32 Declares an object of class CCircle3 Outputs the X, Y coordinate and the RadiusRedefines X, Y and Radius Calls Diameter function from class CCircle3

21  2002 Prentice Hall. All rights reserved. Outline 21 CircleTest.vb 33 ' display CCircle3's circumference 34 output &= "Circumference is " & _ 35 String.Format( "{0:F}", circle.Circumference()) & vbCrLf 36 37 ' display CCircle3's area 38 output &= "Area is " & String.Format( "{0:F}", circle.Area()) 39 40 MessageBox.Show(output, "Demonstrating Class CCircle3" ) 41 End Sub ' Main 42 43 End Module ' modCircleTest3 Calls Circumference and Area function from class CCircle3

22  2002 Prentice Hall. All rights reserved. Outline 22 Circle4.vb 1 ' Fig. 9.12: Circle4.vb 2 ' CCircle4 class that inherits from class CPoint. 3 4 Public Class CCircle4 5 Inherits CPoint ' CCircle4 Inherits from class CPoint 6 7 Private mRadius As Double 8 9 ' default constructor 10 Public Sub New() 11 12 ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New 15 16 ' constructor 17 Public Sub New(ByVal xValue As Integer, _ 18 ByVal yValue As Integer, ByVal radiusValue As Double) 19 20 ' use MyBase reference to CPoint constructor explicitly 21 MyBase.New(xValue, yValue) 22 Radius = radiusValue 23 End Sub ' New 24 25 ' property Radius 26 Public Property Radius() As Double 27 28 Get 29 Return mRadius 30 End Get 31 32 Set(ByVal radiusValue As Double) 33 Class CCircle4 inherits from class CPoint MyBase.New invokes the CPoint constructor explicitly

23  2002 Prentice Hall. All rights reserved. Outline 23 Circle4.vb 34 If radiusValue > 0 35 mRadius = radiusValue 36 End If 37 38 End Set 39 40 End Property ' Radius 41 42 ' calculate CCircle diameter 43 Public Function Diameter() As Double 44 Return mRadius * 2 45 End Function ' Diameter 46 47 ' calculate CCircle4 circumference 48 Public Function Circumference() As Double 49 Return Math.PI * Diameter() 50 End Function ' Circumference 51 52 ' calculate CCircle4 area 53 Public Overridable Function Area() As Double 54 Return Math.PI * mRadius ^ 2 55 End Function ' Area 56 57 ' return String representation of CCircle4 58 Public Overrides Function ToString() As String 59 60 ' use MyBase reference to return CPoint String representation 61 Return "Center= " & MyBase.ToString() & _ 62 "; Radius = " & mRadius 63 End Function ' ToString 64 65 End Class ' CCircle4 Function ToString overrides function ToString in class CPoint’s

24  2002 Prentice Hall. All rights reserved. Outline 24 CircleTest4.vb 1 ' Fig. 9.13: CircleTest4.vb 2 ' Testing class CCircle4. 3 4 Imports System.Windows.Forms 5 6 Module modCircleTest4 7 8 Sub Main() 9 Dim circle As CCircle4 10 Dim output As String 11 12 circle = New CCircle4(37, 43, 2.5) ' instantiate CCircle4 13 14 ' get CCircle4's initial x-y coordinates and radius 15 output = "X coordinate is " & circle.X & vbCrLf & _ 16 "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _ 17 circle.Radius 18 19 ' set CCircle4's x-y coordinates and radius to new values 20 circle.X = 2 21 circle.Y = 2 22 circle.Radius = 4.25 23 24 ' display CCircle4's String representation 25 output &= vbCrLf & vbCrLf & _ 26 "The new location and radius of circle are " & _ 27 vbCrLf & circle.ToString() & vbCrLf 28 29 ' display CCircle4's diameter 30 output &= "Diameter is " & _ 31 String.Format("{0:F}", circle.Diameter()) & vbCrLf 32

25  2002 Prentice Hall. All rights reserved. Outline 25 CircleTest4.vb 33 ' display CCircle4's circumference 34 output &= "Circumference is " & _ 35 String.Format("{0:F}", circle.Circumference()) & vbCrLf 36 37 ' display CCircle4's area 38 output &= "Area is " & String.Format("{0:F}", circle.Area()) 39 40 MessageBox.Show(output, "Demonstrating Class CCircle4") 41 End Sub ' Main 42 43 End Module ' modCircleTest4

26  2002 Prentice Hall. All rights reserved. 26 9.5 Case Study: Three-Level Inheritance Hierarchy Inheritance Example: Point-Circle-Cylinder –Point Class CPoint’s Contains instance variables as Private It also contains properties X and Y for accessing mX, mY and method ToString –Circle Class CCircle4 contains the CPoint functionality It also contains property for Radius and methods Diameter, Circumference, Area, and ToString –Cylinder Class CCylinder contains the CCircle functionality It also contains class CPoint indirectly, and it has CCycinlinder constructor method, property Height and method Volume

27  2002 Prentice Hall. All rights reserved. Outline 27 Cylinder.vb 1 ' Fig. 9.14: Cylinder.vb 2 ' CCylinder class inherits from class CCircle4. 3 4 Public Class CCylinder 5 Inherits CCircle4 6 7 Protected mHeight As Double 8 9 ' default constructor 10 Public Sub New() 11 Height = 0 12 End Sub ' New 13 14 ' four-argument constructor 15 Public Sub New(ByVal xValue As Integer, _ 16 ByVal yValue As Integer, ByVal radiusValue As Double, _ 17 ByVal heightValue As Double) 18 19 ' explicit call to CCircle4 constructor 20 MyBase.New(xValue, yValue, radiusValue) 21 Height = heightValue ' set CCylinder height 22 End Sub ' New 23 24 ' property Height 25 Public Property Height() As Double 26 27 Get 28 Return mHeight 29 End Get 30 31 ' set CCylinder height if argument value is positive 32 Set(ByVal heightValue As Double) 33 Class CCylinder inherits CCircle4 MyBase.New invokes the CCircle4 constructor explicitly

28  2002 Prentice Hall. All rights reserved. Outline 28 Cylinder.vb 34 If heightValue >= 0 Then 35 mHeight = heightValue 36 End If 37 38 End Set 39 40 End Property ' Height 41 42 ' override method Area to calculate CCylinder area 43 Public Overrides Function Area() As Double 44 Return 2 * MyBase.Area + MyBase.Circumference * mHeight 45 End Function ' Area 46 47 ' calculate CCylinder volume 48 Public Function Volume() As Double 49 Return MyBase.Area * mHeight 50 End Function ' Volume 51 52 ' convert CCylinder to String 53 Public Overrides Function ToString() As String 54 Return MyBase.ToString() & "; Height = " & mHeight 55 End Function ' ToString 56 57 End Class ' CCylinder Method Area overrides method Area in class CCircle4 Method ToString overrides method ToString of class CCircle4

29  2002 Prentice Hall. All rights reserved. Outline 29 CylinderTest.vb 1 ' Fig. 9.15: CylinderTest.vb 2 ' Tests class CCylinder. 3 4 Imports System.Windows.Forms 5 6 Module modCylinderTest 7 8 Sub Main() 9 10 ' instantiate object of class CCylinder 11 Dim cylinder As New CCylinder(12, 23, 2.5, 5.7) 12 Dim output As String 13 14 ' properties get initial x-y coordinate, radius and height 15 output = "X coordinate is " & cylinder.X & vbCrLf & _ 16 "Y coordinate is " & cylinder.Y & vbCrLf & "Radius is " & _ 17 cylinder.Radius & vbCrLf & "Height is " & cylinder.Height 18 19 ' properties set new x-y coordinate, radius and height 20 cylinder.X = 2 21 cylinder.Y = 2 22 cylinder.Height = 10 23 cylinder.Radius = 4.25 24 25 ' get new x-y coordinate and radius 26 output &= vbCrLf & vbCrLf & "The new location, radius " & _ 27 "and height of cylinder are" & vbCrLf & "Center = [" & _ 28 cylinder.ToString() & vbCrLf & vbCrLf 29 30 ' display CCylinder's diameter 31 output &= "Diameter is " & _ 32 String.Format("{0:F}", cylinder.Diameter()) & vbCrLf 33 Declares an object of class CCylinder Outputs values of X, Y, Radius, and Height indirectly Resets the values of the cylinder Invokes method Diameter

30  2002 Prentice Hall. All rights reserved. Outline 30 CylinderTest.vb 34 ' display CCylinder's circumference 35 output &= "Circumference is " & _ 36 String.Format("{0:F}", cylinder.Circumference()) & vbCrLf 37 38 ' display CCylinder's area 39 output &= "Area is " & _ 40 String.Format("{0:F}", cylinder.Area()) & vbCrLf 41 42 ' display CCylinder's volume 43 output &= "Volume is " & _ 44 String.Format("{0:F}", cylinder.Volume()) 45 46 MessageBox.Show(output, "Demonstrating Class CCylinder") 47 End Sub ' Main 48 49 End Module ' modCylinderTest Invokes methods Area, Volume and Circumference

31  2002 Prentice Hall. All rights reserved. 31 9.6 Constructors and Finalizers in Derived Classes Constructors in Derived Classes –Base-class Base-class constructors are not inherited by derived classes Each base-class constructor initializes the base-class instance variables that the derived-class object inherits Finalizers in Derived Classes –Derived Classes Executing the finalizer method should free all resources acquired by the object before the garbage collector reclaims memory for that object Keyword MyBase is used to invoke the finalizer of the base class

32  2002 Prentice Hall. All rights reserved. Outline 32 Point3.vb 1 ' Fig. 9.16: Point3.vb 2 ' CPoint3 class represents an x-y coordinate pair. 3 4 Public Class CPoint3 5 6 ' point coordinate 7 Private mX, mY As Integer 8 9 ' default constructor 10 Public Sub New() 11 12 ' implicit call to Object constructor occurs here 13 X = 0 14 Y = 0 15 Console.Writeline("CPoint3 constructor: {0}", Me) 16 End Sub ' New 17 18 ' constructor 19 Public Sub New(ByVal xValue As Integer, _ 20 ByVal yValue As Integer) 21 22 ' implicit call to Object constructor occurs here 23 X = xValue 24 Y = yValue 25 Console.Writeline("CPoint3 constructor: {0}", Me) 26 End Sub ' New 27 28 ' finalizer overrides version in class Object 29 Protected Overrides Sub Finalize() 30 Console.Writeline("CPoint3 Finalizer: {0}", Me) 31 MyBase.Finalize() ' call Object finalizer 32 End Sub ' Finalize 33 Me implicitly invokes the class’s ToString method to obtain the String representation of CPoint3’s coordinates Calls method Finalize explicitly

33  2002 Prentice Hall. All rights reserved. Outline 33 Point3.vb 34 ' property X 35 Public Property X() As Integer 36 37 Get 38 Return mX 39 End Get 40 41 Set(ByVal xValue As Integer) 42 mX = xValue ' no need for validation 43 End Set 44 45 End Property ' X 46 47 ' property Y 48 Public Property Y() As Integer 49 50 Get 51 Return mY 52 End Get 53 54 Set(ByVal yValue As Integer) 55 mY = yValue ' no need for validation 56 End Set 57 58 End Property ' Y 59 60 ' return String representation of CPoint3 61 Public Overrides Function ToString() As String 62 Return "[" & mX & ", " & mY & "]" 63 End Function ' ToString 64 65 End Class ' CPoint3

34  2002 Prentice Hall. All rights reserved. Outline 34 1 ' Fig. 9.17: Circle5.vb 2 ' CCircle5 class that inherits from class CPoint3. 3 4 Public Class CCircle5 5 Inherits CPoint3 ' CCircle5 Inherits from class CPoint3 6 7 Private mRadius As Double 8 9 ' default constructor 10 Public Sub New() 11 12 ' implicit call to CPoint3 constructor occurs here 13 Radius = 0 14 Console.WriteLine("CCircle5 constructor: {0}", Me) 15 End Sub ' New 16 17 ' constructor 18 Public Sub New(ByVal xValue As Integer, _ 19 ByVal yValue As Integer, ByVal radiusValue As Double) 20 21 ' use MyBase reference to CPoint3 constructor explicitly 22 MyBase.New(xValue, yValue) 23 Radius = radiusValue 24 Console.WriteLine("CCircle5 constructor: {0}", Me) 25 End Sub ' New 26 27 ' finalizer overrides version in class CPoint3 28 Protected Overrides Sub Finalize() 29 Console.Writeline("CCircle5 Finalizer: {0}", Me) 30 MyBase.Finalize() ' call CPoint3 finalizer 31 End Sub ' Finalize 32 33 ' property Radius 34 Public Property Radius() As Double 35 Modified constructor from class CPoint3 that includes Radius Modified constructor from class CPoint3 that includes radiusValue MyBase is keyword that invokes method Finalize

35  2002 Prentice Hall. All rights reserved. Outline 35 Circle5.vb 36 Get 37 Return mRadius 38 End Get 39 40 Set(ByVal radiusValue As Double) 41 42 If radiusValue > 0 43 mRadius = radiusValue 44 End If 45 46 End Set 47 48 End Property ' Radius 49 50 ' calculate CCircle5 diameter 51 Public Function Diameter() As Double 52 Return mRadius * 2 53 End Function ' Diameter 54 55 ' calculate CCircle5 circumference 56 Public Function Circumference() As Double 57 Return Math.PI * Diameter() 58 End Function ' Circumference 59 60 ' calculate CCircle5 area 61 Public Overridable Function Area() As Double 62 Return Math.PI * mRadius ^ 2 63 End Function ' Area 64 Calculates Diameter, Circumference and Area Ensures that variable mRadius cannot be assigned negative value

36  2002 Prentice Hall. All rights reserved. Outline 36 Circle5.vb 65 ' return String representation of CCircle5 66 Public Overrides Function ToString() As String 67 68 ' use MyBase reference to return CPoint3 String 69 Return "Center = " & MyBase.ToString() & _ 70 "; Radius = " & mRadius 71 End Function ' ToString 72 73 End Class ' CCircle5 Method ToString overrides method ToString of class CPoint3

37  2002 Prentice Hall. All rights reserved. Outline 37 1 ' Fig. 9.18: ConstructorAndFinalizer.vb 2 ' Display order in which base-class and derived-class constructors 3 ' and finalizers are called. 4 5 Module modConstructorAndFinalizer 6 7 Sub Main() 8 Dim circle1, circle2 As CCircle5 9 10 circle1 = New CCircle5(72, 29, 4.5) ' instantiate objects 11 circle2 = New CCircle5(5, 5, 10) 12 13 circle1 = Nothing ' mark objects for garbage collection 14 circle2 = Nothing 15 16 System.GC.Collect() ' request garbage collector to execute 17 End Sub ' Main 18 19 End Module ' modConstructorAndFinalizer CPoint3 constructor: Center = [72, 29]; Radius = 0 CCircle5 constructor: Center = [72, 29]; Radius = 4.5 CPoint3 constructor: Center = [5, 5]; Radius = 0 CCircle5 constructor: Center = [5, 5]; Radius = 10 CCircle5 Finalizer: Center = [5, 5]; Radius = 10 CPoint3 Finalizer: Center = [5, 5]; Radius = 10 CCircle5 Finalizer: Center = [72, 29]; Radius = 4.5 CPoint3 Finalizer: Center = [72, 29]; Radius = 4.5 Declares objects of class CCircle5 Assigns it to references circle1 and circle2 Sets circle1 and circle2 to Nothing

38  2002 Prentice Hall. All rights reserved. 38 9.7 Software Engineering with Inheritance Inheritance –Class New classes inherit old classes, hence this method allows programmers to practice software reuse


Download ppt " 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived."

Similar presentations


Ads by Google