Overloaded New in MustInherit Class ...

J

Joe HM

Hello -

I have a question regarding overloading the New() in a MustInherit
Class. The following show the code in question ...

MustInherit Class cAbstract
Public MustOverride Sub print()

Protected mName As String

Public Sub New() ' Why do I have to override this?
mName = "N/A"
End Sub

Public Sub New(ByVal aName As String)
mName = aName
End Sub
End Class

Class cChildOfAbstract
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLine("cChildOfAbstract.print() > mName = '" &
mName & "'")
End Sub
End Class

....
Dim lInstanceOfChild As New cChildOfAbstract("My Name") ' Why is
this not working?
....

First of all, I have to override the New(). Is this because I
overloaded it with the New(ByVal aName As String)?

Then I'm trying to call the overloaded constructor when I instantiate
the cChildOfAbstract but I cannot do that because apparently I have
too many arguments. This works if I override the New(ByVal aName As
String) in the child class but why do I have to do that? I thought I
would inherit that from the base class.

Thanks!
Joe
 
J

James Hahn

What do you mean by 'have to override the New()' - what is the message you
are getting? Constructors aren't ordinary methods and are inherited
differently.

If the base class has no constructors or has a a paramaterless constructor
then you don't need a constructor. But if the base class does not have a
paramaterless constructor then your derived class must have a constructor
and the very first line of the constructor must be a call to the base class
constructor. Perhaps it was this requirement (not met in your sample code)
that made it appear that you were required to have a New(). It all depends
on what the base class's constructors are.
 
F

Family Tree Mike

To the best of my knowledge, constructors are not inheritted in VB.Net. Add
this constructor to your class cChildOfAbstract to accomplish what I think
you want:

Public Sub New(ByVal s As String)
MyBase.New(s)
End Sub

I don't think you need the default constructor in your base class, at least
in what I was running.
 
C

Chris Dunaway

Hello -

I have a question regarding overloading the New() in a MustInherit
Class. The following show the code in question ...

MustInherit Class cAbstract
Public MustOverride Sub print()

Protected mName As String

Public Sub New() ' Why do I have to override this?
mName = "N/A"
End Sub

Public Sub New(ByVal aName As String)
mName = aName
End Sub
End Class

Class cChildOfAbstract
Inherits cAbstract

Public Overrides Sub print()
Console.WriteLine("cChildOfAbstract.print() > mName = '" &
mName & "'")
End Sub
End Class

...
Dim lInstanceOfChild As New cChildOfAbstract("My Name") ' Why is
this not working?
...

First of all, I have to override the New(). Is this because I
overloaded it with the New(ByVal aName As String)?

Then I'm trying to call the overloaded constructor when I instantiate
the cChildOfAbstract but I cannot do that because apparently I have
too many arguments. This works if I override the New(ByVal aName As
String) in the child class but why do I have to do that? I thought I
would inherit that from the base class.

Thanks!
Joe

Constructors are not inherited. You will have to provide the child
constructors that you need.

Chris
 
J

Joe HM

Hello -

The following will cause a "Class 'cChild' must declare a 'Sub New'
because its base class 'cParent' does not have an accessible 'Sub New'
that can be called with no arguments."

MustInherit Class cParent
Public Sub New(ByVal aArgument As String)
MyBase.New()
End Sub
End Class

Class cChild
Inherits cParent
End Class

I thought that by default every class has a parameterless constructor
but I guess I was mistaken. Looks like I have to spell it out.

Thanks,
Joe
 
J

Joe HM

Hello -

I guess I just realized that I don't need an interface to the
overloaded New() with the parameter in the base class. The following
works ...

MustInherit Class cParent
....
End Class

Class cChild
Inherits cParent

Public Sub New(ByVal aArgument As String)
MyBase.New()
...
End Sub
End Class

....

Dim lDummy As cParent
lDummy = New cChild("")

Thanks,
Joe
 
T

Tom Shelton

Hello -

The following will cause a "Class 'cChild' must declare a 'Sub New'
because its base class 'cParent' does not have an accessible 'Sub New'
that can be called with no arguments."

MustInherit Class cParent
Public Sub New(ByVal aArgument As String)
MyBase.New()
End Sub
End Class

Class cChild
Inherits cParent
End Class

I thought that by default every class has a parameterless constructor
but I guess I was mistaken. Looks like I have to spell it out.

Thanks,
Joe

The compiler will only generate a default constructor if you have not
explicitly added one to the class. As soon as you add the constructor:

Public Sub New (ByVal aArgument As String)

The compiler assumes you have explicitly stated your desires, and no longer
generates a default constructor. This is how it works in pretty much all
statically compiled languages... And it makes sense, because a lot of times a
default no argument constructor doesn't make sense for a class.
 

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

Top