Inheritance and Explicit Calls to Base Class Constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Inheritance and Explicit Calls to Base Class Constructors

    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 &= "Circumfere nce is " & _
    String.Format(" {0:F}", circle.Circumfe rence()) & vbCrLf

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

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

    End Module ' modCircleTest3
    ******



  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Inheritance and Explicit Calls to Base Class Constructors

    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(xVal ue, 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


    "John" <John@discussio ns.microsoft.co m> wrote in message
    news:475E0B84-4997-40AF-A510-3F793AFAA99F@mi crosoft.com...
    | 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 &= "Circumfere nce is " & _
    | String.Format(" {0:F}", circle.Circumfe rence()) & vbCrLf
    |
    | ' display CCircle3's area
    | output &= "Area is " & String.Format(" {0:F}", circle.Area())
    |
    | MessageBox.Show (output, "Demonstrat ing Class CCircle3")
    | End Sub ' Main
    |
    | End Module ' modCircleTest3
    | ******
    |
    |
    |


    Comment

    Working...