Inheritance and Explicit Calls to Base Class Constructors

G

Guest

Hi,
Maybe someone can help me with the following:
"The first task by any derived class constructor is to call it’s direct or
indirect base class constructor implicitly or explicitly", reads the
statement.
To test this, I added messageboxes in all constructors indicating which
constructor is called.
There are four constructors which could be called:
1. Base class empty constructor
2. Base class 2-Par. constructor
3. Derived class empty constructor
4. Derived class 3-Par. constructor
I expected that constructors # 2 and 4 would be called but not #1 or #3 (no
instantiation of zero parameter objects).
Why does this happen? (see attachment)

Thanks for your help,

Regards,

John

*********
' Fig. 9.9: Point2.vb
' CPoint2 class contains an x-y coordinate pair as Protected data.

Imports System.Windows.Forms

Public Class CPoint2
' implicitly Inherits Object

' point coordinate
Protected mX, mY As Integer

' default constructor
Public Sub New()

' implicit call to Object constructor occurs here
MessageBox.Show("Implicit call was made!", "CPoint2 empty Constuctor")
X = 0
Y = 0
End Sub ' New

' constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer)

' implicit call to Object constructor occurs here
MessageBox.Show("Implicit call was made!", "CPoint2 2-Par Constuctor")
X = xValue
Y = yValue
End Sub ' New

' property X
Public Property X() As Integer

Get
Return mX
End Get

Set(ByVal xValue As Integer)
mX = xValue ' no need for validation
End Set

End Property ' X

' property Y
Public Property Y() As Integer

Get
Return mY
End Get

Set(ByVal yValue As Integer)
mY = yValue ' no need for validation
End Set

End Property ' Y

' return String representation of CPoint2
Public Overrides Function ToString() As String
Return "[" & mX & ", " & mY & "]"
End Function ' ToString

End Class ' CPoint2
*******
Imports System.Windows.Forms

Public Class CCircle3
Inherits CPoint2 ' CCircle3 Inherits from class CPoint2

Private mRadius As Double ' CCircle3's radius

' default constructor
Public Sub New()

' implicit call to CPoint constructor occurs here
MessageBox.Show("Implicit call was made!", "CCircle3 empty Constuctor")
Radius = 0
End Sub ' New

' constructor
Public Sub New(ByVal xValue As Integer, _
ByVal yValue As Integer, ByVal radiusValue As Double)

' implicit call to CPoint2 constructor occurs here
MessageBox.Show("Implicit call was made!", "CCircle3 3-Par Constuctor")
mX = xValue
mY = yValue
Radius = radiusValue
End Sub ' New

' property Radius
Public Property Radius() As Double

Get
Return mRadius
End Get

Set(ByVal radiusValue As Double)

If radiusValue > 0 Then
mRadius = radiusValue
End If

End Set

End Property ' Radius

' calculate CCircle3 diameter
Public Function Diameter() As Double
Return mRadius * 2
End Function ' Diameter

' calculate CCircle3 circumference
Public Function Circumference() As Double
Return Math.PI * Diameter()
End Function ' Circumference

' calculate CCircle3 area
Public Overridable Function Area() As Double
Return Math.PI * mRadius ^ 2
End Function ' Area

' return String representation of CCircle3
Public Overrides Function ToString() As String
Return "Center = " & "[" & mX & ", " & mY & "]" & _
"; Radius = " & mRadius
End Function ' ToString

End Class ' CCircle3
********
' Fig. 9.11: CircleTest3.vb
' Testing class CCircle3.

Imports System.Windows.Forms

Module modCircleTest3

Sub Main()
Dim circle As CCircle3
Dim output As String

circle = New CCircle3(37, 43, 2.5) ' instantiate CCircle3

' get CCircle3's initial x-y coordinates and radius
output = "X coordinate is " & circle.X & vbCrLf & _
"Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _
circle.Radius

' set CCircle3's x-y coordinates and radius to new values
circle.X = 2
circle.Y = 2
circle.Radius = 4.25

' display CCircle3's String representation
output &= vbCrLf & vbCrLf & _
"The new location and radius of circle are " & _
vbCrLf & circle.ToString() & vbCrLf

' display CCircle3's diameter
output &= "Diameter is " & _
String.Format("{0:F}", circle.Diameter()) & vbCrLf

' display CCircle3's circumference
output &= "Circumference is " & _
String.Format("{0:F}", circle.Circumference()) & vbCrLf

' display CCircle3's area
output &= "Area is " & String.Format("{0:F}", circle.Area())

MessageBox.Show(output, "Demonstrating Class CCircle3")
End Sub ' Main

End Module ' modCircleTest3
******
 
J

Jay B. Harlow [MVP - Outlook]

John,
As you code states, you only have implicit calls to constructors. Implicitly
called constructors are always the default constructor. If you want
constructors #2 & #4 called, then you need to explicitly call them.

Something like:

| ' constructor
| Public Sub New(ByVal xValue As Integer, _
| ByVal yValue As Integer, ByVal radiusValue As Double)

| ' Explicit call to CPoint2 constructor occurs here
MyBase.New(xValue, yValue)

| MessageBox.Show("Implicit call was made!", "CCircle3 3-Par
Constuctor")
| mX = xValue
| mY = yValue
| Radius = radiusValue
| End Sub ' New


--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi,
| Maybe someone can help me with the following:
| "The first task by any derived class constructor is to call it's direct or
| indirect base class constructor implicitly or explicitly", reads the
| statement.
| To test this, I added messageboxes in all constructors indicating which
| constructor is called.
| There are four constructors which could be called:
| 1. Base class empty constructor
| 2. Base class 2-Par. constructor
| 3. Derived class empty constructor
| 4. Derived class 3-Par. constructor
| I expected that constructors # 2 and 4 would be called but not #1 or #3
(no
| instantiation of zero parameter objects).
| Why does this happen? (see attachment)
|
| Thanks for your help,
|
| Regards,
|
| John
|
| *********
| ' Fig. 9.9: Point2.vb
| ' CPoint2 class contains an x-y coordinate pair as Protected data.
|
| Imports System.Windows.Forms
|
| Public Class CPoint2
| ' implicitly Inherits Object
|
| ' point coordinate
| Protected mX, mY As Integer
|
| ' default constructor
| Public Sub New()
|
| ' implicit call to Object constructor occurs here
| MessageBox.Show("Implicit call was made!", "CPoint2 empty
Constuctor")
| X = 0
| Y = 0
| End Sub ' New
|
| ' constructor
| Public Sub New(ByVal xValue As Integer, _
| ByVal yValue As Integer)
|
| ' implicit call to Object constructor occurs here
| MessageBox.Show("Implicit call was made!", "CPoint2 2-Par
Constuctor")
| X = xValue
| Y = yValue
| End Sub ' New
|
| ' property X
| Public Property X() As Integer
|
| Get
| Return mX
| End Get
|
| Set(ByVal xValue As Integer)
| mX = xValue ' no need for validation
| End Set
|
| End Property ' X
|
| ' property Y
| Public Property Y() As Integer
|
| Get
| Return mY
| End Get
|
| Set(ByVal yValue As Integer)
| mY = yValue ' no need for validation
| End Set
|
| End Property ' Y
|
| ' return String representation of CPoint2
| Public Overrides Function ToString() As String
| Return "[" & mX & ", " & mY & "]"
| End Function ' ToString
|
| End Class ' CPoint2
| *******
| Imports System.Windows.Forms
|
| Public Class CCircle3
| Inherits CPoint2 ' CCircle3 Inherits from class CPoint2
|
| Private mRadius As Double ' CCircle3's radius
|
| ' default constructor
| Public Sub New()
|
| ' implicit call to CPoint constructor occurs here
| MessageBox.Show("Implicit call was made!", "CCircle3 empty
Constuctor")
| Radius = 0
| End Sub ' New
|
| ' constructor
| Public Sub New(ByVal xValue As Integer, _
| ByVal yValue As Integer, ByVal radiusValue As Double)
|
| ' implicit call to CPoint2 constructor occurs here
| MessageBox.Show("Implicit call was made!", "CCircle3 3-Par
Constuctor")
| mX = xValue
| mY = yValue
| Radius = radiusValue
| End Sub ' New
|
| ' property Radius
| Public Property Radius() As Double
|
| Get
| Return mRadius
| End Get
|
| Set(ByVal radiusValue As Double)
|
| If radiusValue > 0 Then
| mRadius = radiusValue
| End If
|
| End Set
|
| End Property ' Radius
|
| ' calculate CCircle3 diameter
| Public Function Diameter() As Double
| Return mRadius * 2
| End Function ' Diameter
|
| ' calculate CCircle3 circumference
| Public Function Circumference() As Double
| Return Math.PI * Diameter()
| End Function ' Circumference
|
| ' calculate CCircle3 area
| Public Overridable Function Area() As Double
| Return Math.PI * mRadius ^ 2
| End Function ' Area
|
| ' return String representation of CCircle3
| Public Overrides Function ToString() As String
| Return "Center = " & "[" & mX & ", " & mY & "]" & _
| "; Radius = " & mRadius
| End Function ' ToString
|
| End Class ' CCircle3
| ********
| ' Fig. 9.11: CircleTest3.vb
| ' Testing class CCircle3.
|
| Imports System.Windows.Forms
|
| Module modCircleTest3
|
| Sub Main()
| Dim circle As CCircle3
| Dim output As String
|
| circle = New CCircle3(37, 43, 2.5) ' instantiate CCircle3
|
| ' get CCircle3's initial x-y coordinates and radius
| output = "X coordinate is " & circle.X & vbCrLf & _
| "Y coordinate is " & circle.Y & vbCrLf & "Radius is " & _
| circle.Radius
|
| ' set CCircle3's x-y coordinates and radius to new values
| circle.X = 2
| circle.Y = 2
| circle.Radius = 4.25
|
| ' display CCircle3's String representation
| output &= vbCrLf & vbCrLf & _
| "The new location and radius of circle are " & _
| vbCrLf & circle.ToString() & vbCrLf
|
| ' display CCircle3's diameter
| output &= "Diameter is " & _
| String.Format("{0:F}", circle.Diameter()) & vbCrLf
|
| ' display CCircle3's circumference
| output &= "Circumference is " & _
| String.Format("{0:F}", circle.Circumference()) & vbCrLf
|
| ' display CCircle3's area
| output &= "Area is " & String.Format("{0:F}", circle.Area())
|
| MessageBox.Show(output, "Demonstrating Class CCircle3")
| End Sub ' Main
|
| End Module ' modCircleTest3
| ******
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VB-101 "Me" 6
VB.NET-101 Constructors and Methods 10
VB.NET101 - Constructors and Methods (2) 3
Inheritance 7
Abstract Class inheritance problem 5
Inheritance Trouble 2
OOP Inheritance 8
Class problem 1

Top