Overloading constructor in an abstract nested class

L

Louis Somers

Is it possible (if so how) to override the constructor in an abstract class?

Here is a simplified example of what I'm rtying to do:
Create a windows App, drop a button on the form and doubleclick it.
In the onClick event I have:

Button1.Text = New HelloJohn("Hi").ToString()
'should print "Hi John"

Paste the following under the event handler:


Private Class HelloJohn
Inherits HelloWorld

Public Overrides Sub SetName()
_name = "John"
End Sub
End Class

Private MustInherit Class HelloWorld
Protected _greeting As String
Protected _name As String = "world"

Public Sub New()
_greeting = "hello"
SetName()
End Sub

Public Sub New(ByVal Greeting As String)
_greeting = Greeting
SetName()
End Sub

Public MustOverride Sub SetName()

Public Overrides Function ToString() As String
Return _greeting & " " & _name
End Function
End Class

End Class

In this case I'm using a nested class but I tried it on a public class
(in the same namespace) with the same results.

Any Ideas?
Thanks in advance,
Louis
 
A

Armin Zingler

Louis Somers said:
Is it possible (if so how) to override the constructor in an
abstract class?

I don't see a problem with overriding constructors, just that constructors
are not inherited because they determine how an instance of a class can be
created.


Armin
 
T

Tom Shelton

Is it possible (if so how) to override the constructor in an abstract class?

Here is a simplified example of what I'm rtying to do:
Create a windows App, drop a button on the form and doubleclick it.
In the onClick event I have:

Button1.Text = New HelloJohn("Hi").ToString()
'should print "Hi John"

Paste the following under the event handler:


Private Class HelloJohn
Inherits HelloWorld

Public Overrides Sub SetName()
_name = "John"
End Sub
End Class

Private MustInherit Class HelloWorld
Protected _greeting As String
Protected _name As String = "world"

Public Sub New()
_greeting = "hello"
SetName()
End Sub

Public Sub New(ByVal Greeting As String)
_greeting = Greeting
SetName()
End Sub

Public MustOverride Sub SetName()

Public Overrides Function ToString() As String
Return _greeting & " " & _name
End Function
End Class

End Class

In this case I'm using a nested class but I tried it on a public class
(in the same namespace) with the same results.

Any Ideas?
Thanks in advance,
Louis

Constructors are not inherited.... You need to add the constructors to
the derived class:

private class HelloJohn
Inherits HelloWorld

' you'll need this if you want the default behavior
public sub New()
MyBase.New()
End Sub

public sub New (byval greeting as string)
MyBase.New(greeting)
end sub


public overrides sub SetName()
_name = "John"
end sub
End Class

HTH
 
L

Louis Somers

Tom Shelton schreef:
Constructors are not inherited.... You need to add the constructors to
the derived class:

private class HelloJohn
Inherits HelloWorld

' you'll need this if you want the default behavior
public sub New()
MyBase.New()
End Sub

public sub New (byval greeting as string)
MyBase.New(greeting)
end sub


public overrides sub SetName()
_name = "John"
end sub
End Class

HTH

Thanks!
 

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