472,790 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,790 software developers and data experts.

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 &= "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
******

Nov 21 '05 #1
1 2209
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
"John" <Jo**@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.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 &= "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
| ******
|
|
|
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jochen Zeischka | last post by:
Hello, I just tried something with multiple inheritance and I have a problem with the construction of my object. There's a base class Base, containing an integer. The base class has 2 derived...
4
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
20
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
11
by: needin4mation | last post by:
I know this doesn't have much of a context, but, in this (take from Petzold's Programming Windows with C#) snippet: class DatePlus: Date { public DatePlus(){} public DatePlus(int year, int...
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.