Chapter 9 ObjectOriented Programming Inheritance Outline 9 1
Chapter 9 – Object-Oriented Programming: Inheritance Outline 9. 1 9. 2 9. 3 9. 4 9. 5 9. 6 9. 7 9. 8 9. 9 9. 10 Introduction Base Classes and Derived Classes Protected and Friend Members Relationship between Base Classes and Derived Classes Constructors and Finalizers in Derived Classes Implicit Derived-Class-Object to Base-Class-Object Conversion Software Engineering with Inheritance Composition vs. Inheritance Case Study: Point, Circle, Cylinder Visual Inheritance 2002 Prentice Hall. All rights reserved. 1
2 9. 1 Introduction Fig. 9. 1 2002 Prentice Hall. All rights reserved. Some simple inheritance examples.
3 9. 2 Base Classes and Derived Classes Community. Memeber Employee Faculty Administrator Fig. 9. 2 Student Alumnus Staff Teacher Inheritance hierarchy for university CCommunity. Members. 2002 Prentice Hall. All rights reserved.
4 9. 3 Protected and Friend Members CShape CTwo. Dimensional. Shape CCircle CSquare CTriangle Fig. 9. 3 2002 Prentice Hall. All rights reserved. CThree. Dimensional. Shape CSphere CCube Portion of a CShape class hierarchy. CCylinder
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) 2002 Prentice Hall. All rights reserved. 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ' Fig. 9. 4: Point. vb ' CPoint class represents an x-y coordinate pair. Outline Public Class CPoint ' implicitly Inherits Object ' point coordinate Private m. X, m. Y As Integer ' default constructor Public Sub New() Declares integer variables m. X and m. Y as Private ' implicit call to Object constructor occurs here X = 0 Y = 0 End Sub ' New ' constructor Public Sub New(By. Val x. Value As Integer, _ By. Value As Integer) CPoint. vb Two CPoint Constructors ' implicit call to Object constructor occurs here X = x. Value Y = y. Value End Sub ' New ' property X Public Property X() As Integer Get Return m. X End Get Property of X 2002 Prentice Hall. All rights reserved. 6
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 Outline Set(By. Val x. Value As Integer) m. X = x. Value ' no need for validation End Set End Property ' X ' property Y Public Property Y() As Integer Get Return m. Y End Get Property of Y Set(By. Value As Integer) m. Y = y. Value ' no need for validation End Set Method To. String contains End Property ' Y keyword Overrides CPoint. vb ' return String representation of CPoint Public Overrides Function To. String() As String Return "[" & m. X & ", " & m. Y & "]" End Function ' To. String End Class ' CPoint 2002 Prentice Hall. All rights reserved. 7
1 ' Fig. 9. 5: Point. Test. vb 2 ' Testing class CPoint. 3 4 Imports System. Windows. Forms 5 6 Module mod. Point. Test Instantiates an object of class CPoint 7 8 Sub Main() Sets X and Y coordinates 9 Dim point As CPoint 10 Dim output As String 11 12 Changes values pointof= XNew 115) ' instantiate CPoint object and. CPoint(72, Y 13 14 ' display point coordinates via X and Y properties 15 output = "X coordinate is " & point. X & _ 16 vb. Cr. Lf & "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 Retrieves values X and Y and 21 ' display new point value appends values to String Output 22 output &= vb. Cr. Lf & _ 23 "The new location of point is " & point. To. String() 24 25 Message. Box. Show(output, "Demonstrating Class Point" ) 26 End Sub ' Main 27 Outputs String representation of X and Y 28 End Module ' mod. Point. Test Outline Point. Test. vb 2002 Prentice Hall. All rights reserved. 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ' Fig. 9. 6: Circle. vb ' CCircle class contains x-y coordinate pair and radius. Outline Public Class CCircle ' coordinate of center of CCircle Private m. X, m. Y As Integer Private m. Radius As Double ' CCircle's radius ' default constructor Public Sub New() Declared integer variables m. X and m. Y as Private ' implicit call to Object constructor occurs here X = 0 Y = 0 Radius = 0 End Sub ' New ' constructor Public Sub New(By. Val x. Value As Integer, _ By. Value As Integer, By. Val radius. Value As Double) Circle. vb Two CCircle constructors of Radius ' implicit call to Object constructor occurs here X, Y coordinates and X = x. Value Y = y. Value Radius = radius. Value End Sub ' New ' property X Public Property X() As Integer Get Return m. X End Get Property of X 2002 Prentice Hall. All rights reserved. 9
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 Outline Set(By. Val x. Value As Integer) m. X = x. Value ' no need for validation End Set End Property ' X ' property Y Public Property Y() As Integer Property of Y Get Return m. Y End Get Set(By. Value As Integer) m. Y = y. Value ' no need for validation End Set End Property ' Y Circle. vb ' property Radius Public Property Radius() As Double Get Property of Radius Return m. Radius End Get Set(By. Val radius. Value As Double) If radius. Value > 0 m. Radius = radius. Value End If End Set End Property ' Radius Ensures the member m. Radius cannot be assigned a negative value 2002 Prentice Hall. All rights reserved. 10
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 ' calculate CCircle diameter Public Function Diameter() As Double Return m. Radius * 2 End Function ' Diameter Outline ' calculate CCircle circumference Public Function Circumference() As Double Return Math. PI * Diameter() End Function ' Circumference Methods to calculate Diameter, Circumference, Area, and To. String Double representation ' calculate CCircle area Public Function Area() As Return Math. PI * m. Radius ^ 2 End Function ' Area ' return String representation of CCircle Public Overrides Function To. String() As String Return "Center = " & "[" & m. X & ", " & m. Y & "]" & _ "; Radius = " & m. Radius End Function ' To. String Circle. vb End Class ' CCircle 2002 Prentice Hall. All rights reserved. 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Outline ' Fig. 9. 7: Circle. Test. vb ' Testing class CCircle. 12 Imports System. Windows. Forms Module mod. Circle. Test Sub Main() Dim circle As CCircle Dim output As String Declares an object of class CCircle circle = New CCircle(37, 43, 2. 5) ' instantiate CCircle ' get CCircle's initial x-y coordinates and radius output = "X coordinate is " & circle. X & vb. Cr. Lf & _ "Y coordinate is " & circle. Y & vb. Cr. Lf & "Radius is " & _ circle. Radius ' set CCircle's x-y coordinates and radius to new values circle. X = 2 Retrieves then concatenate values X, circle. Y = 2 Y and Radius to String output circle. Radius = 4. 25 Inheritance. Test. vb display Out puts'Diameter CCircle's String representation output &= vb. Cr. Lf & _ "The new location and radius of circle are " & _ vb. Cr. Lf. Resets & circle. To. String() vb. Cr. Lfby using its properties values X, Y and &Radius ' display CCircle's diameter output &= "Diameter is " & _ String. Format("{0: F}", circle. Diameter()) & vb. Cr. Lf 2002 Prentice Hall. All rights reserved.
33 34 35 36 37 38 39 40 41 42 43 ' display CCircle's circumference output &= "Circumference is " & _ String. Format("{0: F}", circle. Circumference()) & vb. Cr. Lf Outline 13 ' display CCircle's area output &= "Area is " & String. Format("{0: F}", circle. Area()) Message. Box. Show(output, "Demonstrating Class CCircle" ) Outputs circumference and area End Sub ' Main End Module ' mod. Circle. Test Inheritance. Test. vb 2002 Prentice Hall. All rights reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ' Fig. 9. 8: Circle 2. vb ' CCircle 2 class that inherits from class CPoint. Outline Public Class CCircle 2 Inherits CPoint ' CCircle 2 Inherits from class CPoint Private m. Radius As Double ' CCircle 2's radius ' default constructor Public Sub New() The keyword Inherits indicates inheritance CCircle 2 inherits all members of class CPoint except constructors ' implicit call to CPoint constructor occurs here Radius = 0 End Sub ' New ' constructor Public Sub New(By. Val x. Value As Integer, _ By. Value As Integer, By. Val radius. Value As Double) Circle 2. vb ' implicit call to CPoint constructor occurs here m. X = x. Value m. Y = y. Value Radius = radius. Value Generates first syntax error End Sub ' New ' property Radius Public Property Radius() As Double Get Return m. Radius End Get 2002 Prentice Hall. All rights reserved. 14
33 Set(By. Val radius. Value As Double) 34 35 If radius. Value > 0 36 m. Radius = radius. Value 37 End If 38 39 End Set Ensures that variable m. Radius cannot 40 41 End Property ' Radius be assigned a negative value 42 43 ' calculate CCircle 2 diameter 44 Public Function Diameter() As Double 45 Return m. Radius * 2 46 End Function ' Diameter 47 48 ' calculate CCircle 2 circumference 49 Public Function Circumference() As Double 50 Return Math. PI * Diameter() 51 End Function ' Circumference 52 53 ' calculate CCircle 2 area 54 Public Function Area() As Double 55 Return Math. PI * m. Radius ^ 2 56 End Function ' Area 57 58 ' return String representation of CCircle 2 59 Public Overrides Function To. String() As String 60 Return "Center = " & "[" & m. X & ", " & m. Y & "]" & _ 61 "; Radius = " & m. Radius 62 End Function ' To. String Generates second syntax error 63 64 End Class ' CCircle 2 Outline Circle. vb 2002 Prentice Hall. All rights reserved. 15
1 ' Fig. 9. 9: Point 2. vb 2 ' CPoint 2 class contains an x-y coordinate pair as Protected data. 3 4 Public Class CPoint 2 5 ' implicitly Inherits Object 6 7 ' point coordinate 8 Protected m. X, m. Y As Integer 9 10 ' default constructor Class CPoint 2 modifies class CPoint 11 Public Sub New() to declare variables m. X and m. Y as 12 Protectedoccurs here 13 ' implicit call to Object constructor 14 X = 0 15 Y = 0 16 End Sub ' New 17 18 ' constructor 19 Public Sub New(By. Val x. Value As Integer, _ 20 By. Value As Integer) 21 22 ' implicit call to Object constructor occurs here 23 X = x. Value 24 Y = y. Value 25 End Sub ' New 26 27 ' property X 28 Public Property X() As Integer 29 30 Get 31 Return m. X 32 End Get 33 Outline 2002 Prentice Hall. All rights reserved. 16
34 Set(By. Val x. Value As Integer) 35 m. X = x. Value ' 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 m. Y 45 End Get 46 47 Set(By. Value As Integer) 48 m. Y = y. Value ' no need for validation 49 End Set 50 51 End Property ' Y 52 53 ' return String representation of CPoint 2 54 Public Overrides Function To. String() As String 55 Return "[" & m. X & ", " & m. Y & "]" 56 End Function ' To. String 57 58 End Class ' CPoint 2 Outline 2002 Prentice Hall. All rights reserved. 17
1 ' Fig. 9. 10: Circle 3. vb 2 ' CCircle 3 class that inherits from class CPoint 2. 3 4 Public Class CCircle 3 5 Inherits CPoint 2 ' CCircle 3 Inherits from class CPoint 2 6 7 Private m. Radius As Double ' CCircle 3's radius 8 9 ' default constructor 10 Public Sub New() Class CCircle 3 inherits CPoint 2 11 12 ' implicit call to CPoint constructor occurs here 13 Radius = 0 14 End Sub ' New 15 16 ' constructor 17 Public Sub New(By. Val x. Value As Integer, _ 18 By. Value As Integer, By. Val radius. Value As Double) 19 20 ' implicit call to CPoint 2 constructor occurs here 21 m. X = x. Value 22 m. Y = y. Value 23 Radius = radius. Value 24 End Sub ' New 25 26 ' property Radius 27 Public Property Radius() As Double 28 29 Get 30 Return m. Radius 31 End Get 32 33 Set(By. Val radius. Value As Double) 34 Outline Circle 3. vb 2002 Prentice Hall. All rights reserved. 18
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 If radius. Value > 0 m. Radius = radius. Value End If Outline End Set End Property ' Radius ' calculate CCircle 3 diameter Public Function Diameter() As Double Return m. Radius * 2 End Function ' Diameter ' calculate CCircle 3 circumference Public Function Circumference() As Double Return Math. PI * Diameter() End Function ' Circumference ' calculate CCircle 3 area Public Overridable Function Area() As Double Return Math. PI * m. Radius ^ 2 End Function ' Area CCircle. vb ' return String representation of CCircle 3 Public Overrides Function To. String() As String Return "Center = " & "[" & m. X & ", " & m. Y & "]" & _ "; Radius = " & m. Radius End Function ' To. String End Class ' CCircle 3 2002 Prentice Hall. All rights reserved. 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Outline ' Fig. 9. 11: Circle. Test 3. vb ' Testing class CCircle 3. Imports System. Windows. Forms Module mod. Circle. Test 3 Declares an object of class CCircle 3 Sub Main() Dim circle As CCircle 3 Dim output As String circle = New CCircle 3(37, 43, 2. 5) ' instantiate CCircle 3 ' get CCircle 3's initial x-y coordinates and radius output = "X coordinate is " & circle. X & vb. Cr. Lf & _ "Y coordinate is " & circle. Y & vb. Cr. Lf & "Radius is " & _ circle. Radius ' set CCircle 3's x-y coordinates and radius to new values circle. X = 2 Outputs the X, Y coordinate and the Radius circle. Y = 2 circle. Radius = 4. 25 CCircle. vb Calls Diameter function ' display CCircle 3's from class CCircle 3 String representation output &= vb. Cr. Lf & _ Y and are Radius "The new location and Redefines radius of. X, circle " & _ vb. Cr. Lf & circle. To. String() & vb. Cr. Lf ' display CCircle 3's diameter output &= "Diameter is " & _ String. Format("{0: F}", circle. Diameter()) & vb. Cr. Lf 2002 Prentice Hall. All rights reserved. 20
33 ' display CCircle 3's circumference 34 output &= "Circumference is " & _ 35 String. Format("{0: F}", circle. Circumference()) & vb. Cr. Lf 36 37 ' display CCircle 3's area Calls Circumference and Area 38 output &= "Area is " & String. Format("{0: F}", circle. Area()) function from class CCircle 3 39 40 Message. Box. Show(output, "Demonstrating Class CCircle 3" ) 41 End Sub ' Main 42 43 End Module ' mod. Circle. Test 3 Outline Circle. Test. vb 2002 Prentice Hall. All rights reserved. 21
1 ' Fig. 9. 12: Circle 4. vb 2 ' CCircle 4 class that inherits from class CPoint. 3 4 Public Class CCircle 4 5 Inherits CPoint ' CCircle 4 Inherits from class CPoint 6 7 Private m. Radius As Double 8 9 ' default constructor Class CCircle 4 inherits from class CPoint 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(By. Val x. Value As Integer, _ 18 By. Value As Integer, By. Val radius. Value As Double) 19 20 ' use My. Base reference to CPoint constructor explicitly 21 My. Base. New(x. Value, y. Value) 22 Radius = radius. Value 23 End Sub ' New 24 25 ' property Radius My. Base. New invokes the CPoint 26 Public Property Radius() As Double constructor explicitly 27 28 Get 29 Return m. Radius 30 End Get 31 32 Set(By. Val radius. Value As Double) 33 Outline Circle 4. vb 2002 Prentice Hall. All rights reserved. 22
34 If radius. Value > 0 35 m. Radius = radius. Value 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 m. Radius * 2 45 End Function ' Diameter 46 47 ' calculate CCircle 4 circumference 48 Public Function Circumference() As Double 49 Return Math. PI * Diameter() 50 End Function ' Circumference 51 52 ' calculate CCircle 4 area To. String 53 Public Overridable Function Area() As Double overrides function 54 Return Math. PI * m. Radius To. String ^ 2 in class CPoint’s 55 End Function ' Area 56 57 ' return String representation of CCircle 4 58 Public Overrides Function To. String() As String 59 60 ' use My. Base reference to return CPoint String representation 61 Return "Center= " & My. Base. To. String() & _ 62 "; Radius = " & m. Radius 63 End Function ' To. String 64 65 End Class ' CCircle 4 Outline Circle 4. vb 2002 Prentice Hall. All rights reserved. 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ' Fig. 9. 13: Circle. Test 4. vb ' Testing class CCircle 4. Outline Imports System. Windows. Forms Module mod. Circle. Test 4 Sub Main() Dim circle As CCircle 4 Dim output As String circle = New CCircle 4(37, 43, 2. 5) ' instantiate CCircle 4 ' get CCircle 4's initial x-y coordinates and radius output = "X coordinate is " & circle. X & vb. Cr. Lf & _ "Y coordinate is " & circle. Y & vb. Cr. Lf & "Radius is " & _ circle. Radius ' set CCircle 4's x-y coordinates and radius to new values circle. X = 2 circle. Y = 2 circle. Radius = 4. 25 Circle. Test 4. vb ' display CCircle 4's String representation output &= vb. Cr. Lf & _ "The new location and radius of circle are " & _ vb. Cr. Lf & circle. To. String() & vb. Cr. Lf ' display CCircle 4's diameter output &= "Diameter is " & _ String. Format("{0: F}", circle. Diameter()) & vb. Cr. Lf 2002 Prentice Hall. All rights reserved. 24
33 ' display CCircle 4's circumference 34 output &= "Circumference is " & _ 35 String. Format("{0: F}", circle. Circumference()) & vb. Cr. Lf 36 37 ' display CCircle 4's area 38 output &= "Area is " & String. Format("{0: F}", circle. Area()) 39 40 Message. Box. Show(output, "Demonstrating Class CCircle 4" ) 41 End Sub ' Main 42 43 End Module ' mod. Circle. Test 4 Outline Circle. Test 4. vb 2002 Prentice Hall. All rights reserved. 25
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 m. X, m. Y and method To. String – Circle • Class CCircle 4 contains the CPoint functionality • It also contains property for Radius and methods Diameter, Circumference, Area, and To. String – Cylinder • Class CCylinder contains the CCircle functionality • It also contains class CPoint indirectly, and it has CCycinlinder constructor method, property Height and method Volume 2002 Prentice Hall. All rights reserved. 26
1 ' Fig. 9. 14: Cylinder. vb 2 ' CCylinder class inherits from class CCircle 4. 3 4 Public Class CCylinder 5 Inherits CCircle 4 6 7 Protected m. Height As Double 8 Class CCylinder inherits CCircle 4 9 ' default constructor 10 Public Sub New() 11 Height = 0 12 End Sub ' New 13 14 ' four-argument constructor 15 Public Sub New(By. Val x. Value As Integer, _ 16 By. Value As Integer, By. Val radius. Value As Double, _ 17 By. Val height. Value As Double) 18 19 ' explicit call to CCircle 4 constructor 20 My. Base. New(x. Value, y. Value, radius. Value) 21 Height = height. Value ' set CCylinder height 22 End Sub ' New 23 24 ' property Height My. Base. New invokes the CCircle 4 25 Public Property Height() As Double constructor explicitly 26 27 Get 28 Return m. Height 29 End Get 30 31 ' set CCylinder height if argument value is positive 32 Set(By. Val height. Value As Double) 33 Outline Cylinder. vb 2002 Prentice Hall. All rights reserved. 27
34 If height. Value >= 0 Then 35 m. Height = height. Value 36 End If 37 Method Area overrides method 38 End Set Area in class CCircle 4 39 40 End Property ' Height 41 42 ' override method Area to calculate CCylinder area 43 Public Overrides Function Area() As Double 44 Return 2 * My. Base. Area + My. Base. Circumference * m. Height 45 End Function ' Area 46 47 ' calculate CCylinder volume 48 Public Function Volume() As Double Method To. String overrides method 49 Return My. Base. Area * m. Height To. String of class CCircle 4 50 End Function ' Volume 51 52 ' convert CCylinder to String 53 Public Overrides Function To. String() As String 54 Return My. Base. To. String() & "; Height = " & m. Height 55 End Function ' To. String 56 57 End Class ' CCylinder Outline Cylinder. vb 2002 Prentice Hall. All rights reserved. 28
1 ' Fig. 9. 15: Cylinder. Test. vb 2 ' Tests class CCylinder. 3 4 Imports System. Windows. Forms 5 Declares an object of class CCylinder 6 Module mod. Cylinder. Test 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 & vb. Cr. Lf & _ 16 "Y coordinate is " & cylinder. Y & vb. Cr. Lf & "Radius is " & _ 17 cylinder. Radius & vb. Cr. Lf & "Height is " & cylinder. Height 18 19 ' properties set new x-y coordinate, radius and height 20 cylinder. X = 2 Outputs values of X, Y, Radius, and 21 cylinder. Y = 2 22 cylinder. Height = 10 Height indirectly 23 cylinder. Radius = 4. 25 Invokes method Diameter 24 25 ' get new x-y coordinate and radius 26 output &= vb. Cr. Lf & "The new location, radius " & _ 27 "and height of cylinder are" & vb. Cr. Lf "Center = [" & _ Resets the values of the &cylinder 28 cylinder. To. String() & vb. Cr. Lf 29 30 ' display CCylinder's diameter 31 output &= "Diameter is " & _ 32 String. Format("{0: F}", cylinder. Diameter()) & vb. Cr. Lf 33 Outline Cylinder. Test. vb 2002 Prentice Hall. All rights reserved. 29
34 ' display CCylinder's circumference 35 output &= "Circumference is " & _ 36 String. Format("{0: F}", cylinder. Circumference()) & vb. Cr. Lf 37 38 ' display CCylinder's area 39 output &= "Area is " & _ 40 String. Format("{0: F}", cylinder. Area()) & vb. Cr. Lf 41 methods Area, Volume and 42 Invokes ' display CCylinder's volume 43 Circumference output &= "Volume is " & _ 44 String. Format("{0: F}", cylinder. Volume()) 45 46 Message. Box. Show(output, "Demonstrating Class CCylinder" ) 47 End Sub ' Main 48 49 End Module ' mod. Cylinder. Test Outline Cylinder. Test. vb 2002 Prentice Hall. All rights reserved. 30
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 My. Base is used to invoke the finalizer of the base class 2002 Prentice Hall. All rights reserved. 31
1 ' Fig. 9. 16: Point 3. vb 2 ' CPoint 3 class represents an x-y coordinate pair. 3 4 Public Class CPoint 3 5 6 ' point coordinate 7 Private m. X, m. Y 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("CPoint 3 constructor: {0}" , Me) 16 End Sub ' New 17 18 ' constructor Me implicitly invokes 19 Public Sub New(By. Val x. Value As Integer, _ the class’s To. String 20 By. Value As Integer) method to obtain the String representation 21 of CPoint 3’s coordinates 22 ' implicit call to Object constructor occurs here 23 X = x. Value 24 Y = y. Value 25 Console. Writeline("CPoint 3 constructor: {0}" , Me) Calls method Finalize explicitly 26 End Sub ' New 27 28 ' finalizer overrides version in class Object 29 Protected Overrides Sub Finalize() 30 Console. Writeline("CPoint 3 Finalizer: {0}" , Me) 31 My. Base. Finalize() ' call Object finalizer 32 End Sub ' Finalize 33 Outline Point 3. vb 2002 Prentice Hall. All rights reserved. 32
34 ' property X 35 Public Property X() As Integer 36 37 Get 38 Return m. X 39 End Get 40 41 Set(By. Val x. Value As Integer) 42 m. X = x. Value ' 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 m. Y 52 End Get 53 54 Set(By. Value As Integer) 55 m. Y = y. Value ' no need for validation 56 End Set 57 58 End Property ' Y 59 60 ' return String representation of CPoint 3 61 Public Overrides Function To. String() As String 62 Return "[" & m. X & ", " & m. Y & "]" 63 End Function ' To. String 64 65 End Class ' CPoint 3 Outline Point 3. vb 2002 Prentice Hall. All rights reserved. 33
Outline 1 ' Fig. 9. 17: Circle 5. vb 2 ' CCircle 5 class that inherits from class CPoint 3. 3 4 Public Class CCircle 5 5 Inherits CPoint 3 ' CCircle 5 Inherits from class CPoint 3 6 7 Private m. Radius As Double 8 9 ' default constructor Modified constructor from class CPoint 3 that includes Radius 10 Public Sub New() 11 12 ' implicit call to CPoint 3 constructor occurs here 13 Radius = 0 14 Console. Write. Line("CCircle 5 constructor: {0}" , Me) 15 End Sub ' New 16 Modified constructor from class CPoint 3 17 ' constructor that radius. Value 18 Public Sub New(By. Val x. Value Asincludes Integer, _ 19 By. Value As Integer, By. Val radius. Value As Double) 20 21 ' use My. Base reference to CPoint 3 constructor explicitly 22 My. Base. New(x. Value, y. Value) 23 Radius = radius. Value 24 Console. Write. Line("CCircle 5 constructor: {0}" , Me) 25 End Sub ' New 26 My. Base is keyword invokes method Finalize 27 ' finalizer overrides version in class that CPoint 3 28 Protected Overrides Sub Finalize() 29 Console. Writeline("CCircle 5 Finalizer: {0}" , Me) 30 My. Base. Finalize() ' call CPoint 3 finalizer 31 End Sub ' Finalize 32 33 ' property Radius 34 Public Property Radius() As Double 2002 Prentice Hall. 35 All rights reserved. 34
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 Outline Get Return m. Radius End Get Ensures that variable m. Radius cannot be assigned negative value Set(By. Val radius. Value As Double) If radius. Value > 0 m. Radius = radius. Value End If End Set Calculates Diameter, Circumference and Area End Property ' Radius ' calculate CCircle 5 diameter Public Function Diameter() As Double Return m. Radius * 2 End Function ' Diameter Circle 5. vb ' calculate CCircle 5 circumference Public Function Circumference() As Double Return Math. PI * Diameter() End Function ' Circumference ' calculate CCircle 5 area Public Overridable Function Area() As Double Return Math. PI * m. Radius ^ 2 End Function ' Area 2002 Prentice Hall. All rights reserved. 35
65 ' return String representation of CCircle 5 66 Public Overrides Function To. String() As String 67 68 ' use My. Base reference to return CPoint 3 String 69 Return "Center = " & My. Base. To. String() & _ Method To. String overrides 70 "; Radius = " & m. Radius 71 End Function ' To. String of class CPoint 3 72 73 End Class ' CCircle 5 Outline method Circle 5. vb 2002 Prentice Hall. All rights reserved. 36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ' Fig. 9. 18: Constructor. And. Finalizer. vb ' Display order in which base-class and derived-class constructors ' and finalizers are called. Outline Module mod. Constructor. And. Finalizer Declares objects of class CCircle 5 Assigns it to references circle 1 and circle 2 Sub Main() Dim circle 1, circle 2 As CCircle 5 circle 1 = New CCircle 5(72, 29, 4. 5) ' instantiate objects circle 2 = New CCircle 5(5, 5, 10) circle 1 = Nothing ' mark objects for garbage collection circle 2 = Nothing System. GC. Collect() ' request garbage collector to execute Sets circle 1 and circle 2 to Nothing End Sub ' Main End Module ' mod. Constructor. And. Finalizer CPoint 3 constructor: Center = [72, 29]; Radius = 0 CCircle 5 constructor: Center = [72, 29]; Radius = 4. 5 CPoint 3 constructor: Center = [5, 5]; Radius = 0 CCircle 5 constructor: Center = [5, 5]; Radius = 10 CCircle 5 Finalizer: Center = [5, 5]; Radius = 10 CPoint 3 Finalizer: Center = [5, 5]; Radius = 10 CCircle 5 Finalizer: Center = [72, 29]; Radius = 4. 5 CPoint 3 Finalizer: Center = [72, 29]; Radius = 4. 5 2002 Prentice Hall. All rights reserved. 37
38 9. 7 Software Engineering with Inheritance • Inheritance – Class • New classes inherit old classes, hence this method allows programmers to practice software reuse 2002 Prentice Hall. All rights reserved.
- Slides: 38