473,396 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Overloading constructor in an abstract nested class

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
Jan 4 '08 #1
3 1350
On 2008-01-04, Louis Somers <no*****@dontspamme.nowrote:
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

--
Tom Shelton
Jan 4 '08 #2
"Louis Somers" <no*****@dontspamme.noschrieb
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

Jan 4 '08 #3
Tom Shelton schreef:
On 2008-01-04, Louis Somers <no*****@dontspamme.nowrote:
>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
Thanks!
Jan 4 '08 #4

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

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
6
by: away | last post by:
Why some classes are seen to has their constructors declared as "protected"? Thanks!
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
22
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
2
by: tshad | last post by:
Using VB.Net VS 2003 I am getting the following message when I call my 2nd constructor. Too many arguments to 'Public Sub New()'. I have 2 constructors: **********************************...
1
by: Stodge | last post by:
Yet another SWIG question (YASQ!). I'm having a problem with using an abstract base class. When generating the Python bindings, SWIG thinks that all the concrete classes that derive from this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.