Overriding Methods Question
-
Greetings, My hope is that someone can help a newbie trying to learn this OOP on her own. My base class is POINT2 and the derived class is POINT3 and I'm trying to overide the subroutine called add. As the code is written - I get the error sub "add" cannot be declared 'Overrides' because it does not overrides a sub in a base class and it recommends to me to remove the keyword Overrides. Once I remove the keyword Overrides, I get the error message, sub 'add' shadows an overridable method in the base class 'POINT2'. To override the base method, this method must be declared 'Overrides'. Now I'm not pointing the finger at Microsoft, I'm sure it is something I did or didn't do. Help me stop chasing my tail. Karen Nooobie to OOP and VB.Net 2005 (beta)
' 2D POINT =========================================================== Public Class POINT2 Friend _X As Decimal = Nothing Friend _Y As Decimal = Nothing Public Sub New(ByVal x As Decimal, ByVal y As Decimal) mybase.new() Me._X = x Me._Y = y End Sub ' Constructor Property X() Get Return (_X) End Get Set(ByVal value) _X = value End Set End Property ' Get/Set X Property Y() Get Return (_Y) End Get Set(ByVal value) _Y = value End Set End Property ' Get/Set Y Public Overridable Sub add(ByRef p1 As POINT2) Me._X += p1.X Me._Y += p1.Y End Sub End Class ' 2D POINT Class ' 3D POINT =========================================================== Public Class POINT3 Inherits POINT2 Friend _Z As Decimal = Nothing Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal) MyBase.New(x, y) Me._X = x Me._Y = y Me._Z = z End Sub ' Constructor Property Z() Get Return (_Z) End Get Set(ByVal value) _Z = value End Set End Property ' Get/Set Z Public **Overrides** Sub
-
Greetings, My hope is that someone can help a newbie trying to learn this OOP on her own. My base class is POINT2 and the derived class is POINT3 and I'm trying to overide the subroutine called add. As the code is written - I get the error sub "add" cannot be declared 'Overrides' because it does not overrides a sub in a base class and it recommends to me to remove the keyword Overrides. Once I remove the keyword Overrides, I get the error message, sub 'add' shadows an overridable method in the base class 'POINT2'. To override the base method, this method must be declared 'Overrides'. Now I'm not pointing the finger at Microsoft, I'm sure it is something I did or didn't do. Help me stop chasing my tail. Karen Nooobie to OOP and VB.Net 2005 (beta)
' 2D POINT =========================================================== Public Class POINT2 Friend _X As Decimal = Nothing Friend _Y As Decimal = Nothing Public Sub New(ByVal x As Decimal, ByVal y As Decimal) mybase.new() Me._X = x Me._Y = y End Sub ' Constructor Property X() Get Return (_X) End Get Set(ByVal value) _X = value End Set End Property ' Get/Set X Property Y() Get Return (_Y) End Get Set(ByVal value) _Y = value End Set End Property ' Get/Set Y Public Overridable Sub add(ByRef p1 As POINT2) Me._X += p1.X Me._Y += p1.Y End Sub End Class ' 2D POINT Class ' 3D POINT =========================================================== Public Class POINT3 Inherits POINT2 Friend _Z As Decimal = Nothing Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal) MyBase.New(x, y) Me._X = x Me._Y = y Me._Z = z End Sub ' Constructor Property Z() Get Return (_Z) End Get Set(ByVal value) _Z = value End Set End Property ' Get/Set Z Public **Overrides** Sub
In your implementation, you're adding functionality to a base class, POINT2, not overriding it. The base class supports every associated with POINT2, including it's Add method. Your Add method takes a parameter of POINT2 pass ByRef. In your POINT3 class, your Add method is taking a parameter of POINT3 ByRef. This is a different method from the base class. So what you're really doing is adding new support to the Add method in POINT2, not replacing it. This is called Overloading. You have multiple methods with the same name that take different parameters. Calling the Add method with a POINT2 parameter will call the POINT2 class' version. Calling the Add method with a POINT3 parameter will call the POINT3 subclass' Add method. A few things need to be changed in your code. We'll start with POINT2:
' 2D POINT ===========================================================
Public Class POINT2
'Friend _X As Decimal = Nothing ' Remove the "= Nothing"'s. Decimals are value types that
'Friend _Y As Decimal = Nothing ' can't be set to Nothing. Also, you might want to change
' these to Doubles instead. Decimal types are 128-bit numbers
' and a rather slow to work with.
Friend _X As Double
Friend _Y As Double
Public Sub New(ByVal x As Double, ByVal y As Double)
'mybase.new() ' Since this class isn't inheriting from anything (well, not really true!),
' you're calling the New of System.Object, which doesn't
' do anything. Remove this line.
'Me._X = x ' You should be setting your initial values using your
'Me._Y = y ' Property statements. Your Properties can validate the data
' and return Exceptions if not valid.
Me.X = x
Me.Y = y
End Sub ' Constructor
Public Property X() As Double ' It doesn't hurt to be specific about what your coding.
Get ' Making assumptions and not specifying exactly what you
Return Me._X ' want Public/Private can lead to hard to find problems.
End Get
Set(ByVal value)
' You might want to do some checking of your values to see if they
' are valid before setting the value of this class instance.
Me._X = value
End Set -
In your implementation, you're adding functionality to a base class, POINT2, not overriding it. The base class supports every associated with POINT2, including it's Add method. Your Add method takes a parameter of POINT2 pass ByRef. In your POINT3 class, your Add method is taking a parameter of POINT3 ByRef. This is a different method from the base class. So what you're really doing is adding new support to the Add method in POINT2, not replacing it. This is called Overloading. You have multiple methods with the same name that take different parameters. Calling the Add method with a POINT2 parameter will call the POINT2 class' version. Calling the Add method with a POINT3 parameter will call the POINT3 subclass' Add method. A few things need to be changed in your code. We'll start with POINT2:
' 2D POINT ===========================================================
Public Class POINT2
'Friend _X As Decimal = Nothing ' Remove the "= Nothing"'s. Decimals are value types that
'Friend _Y As Decimal = Nothing ' can't be set to Nothing. Also, you might want to change
' these to Doubles instead. Decimal types are 128-bit numbers
' and a rather slow to work with.
Friend _X As Double
Friend _Y As Double
Public Sub New(ByVal x As Double, ByVal y As Double)
'mybase.new() ' Since this class isn't inheriting from anything (well, not really true!),
' you're calling the New of System.Object, which doesn't
' do anything. Remove this line.
'Me._X = x ' You should be setting your initial values using your
'Me._Y = y ' Property statements. Your Properties can validate the data
' and return Exceptions if not valid.
Me.X = x
Me.Y = y
End Sub ' Constructor
Public Property X() As Double ' It doesn't hurt to be specific about what your coding.
Get ' Making assumptions and not specifying exactly what you
Return Me._X ' want Public/Private can lead to hard to find problems.
End Get
Set(ByVal value)
' You might want to do some checking of your values to see if they
' are valid before setting the value of this class instance.
Me._X = value
End Set -
In your implementation, you're adding functionality to a base class, POINT2, not overriding it. The base class supports every associated with POINT2, including it's Add method. Your Add method takes a parameter of POINT2 pass ByRef. In your POINT3 class, your Add method is taking a parameter of POINT3 ByRef. This is a different method from the base class. So what you're really doing is adding new support to the Add method in POINT2, not replacing it. This is called Overloading. You have multiple methods with the same name that take different parameters. Calling the Add method with a POINT2 parameter will call the POINT2 class' version. Calling the Add method with a POINT3 parameter will call the POINT3 subclass' Add method. A few things need to be changed in your code. We'll start with POINT2:
' 2D POINT ===========================================================
Public Class POINT2
'Friend _X As Decimal = Nothing ' Remove the "= Nothing"'s. Decimals are value types that
'Friend _Y As Decimal = Nothing ' can't be set to Nothing. Also, you might want to change
' these to Doubles instead. Decimal types are 128-bit numbers
' and a rather slow to work with.
Friend _X As Double
Friend _Y As Double
Public Sub New(ByVal x As Double, ByVal y As Double)
'mybase.new() ' Since this class isn't inheriting from anything (well, not really true!),
' you're calling the New of System.Object, which doesn't
' do anything. Remove this line.
'Me._X = x ' You should be setting your initial values using your
'Me._Y = y ' Property statements. Your Properties can validate the data
' and return Exceptions if not valid.
Me.X = x
Me.Y = y
End Sub ' Constructor
Public Property X() As Double ' It doesn't hurt to be specific about what your coding.
Get ' Making assumptions and not specifying exactly what you
Return Me._X ' want Public/Private can lead to hard to find problems.
End Get
Set(ByVal value)
' You might want to do some checking of your values to see if they
' are valid before setting the value of this class instance.
Me._X = value
End SetDave Kreskowiak wrote:
Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal) 'MyBase.New(x, y) ' This line isn't required because you're setting all ' three values in this constructor. Anything you do in ' in the POINT2 constructor would just be overwritten ' by what you have in this one. Me.X = x ' Again, go through the Properties! Me.Y = y Me.Z = z End Sub ' Constructor
Dave, As soon as I comment out the 'Mybase.New()' statement in POINT3 class, VS2005 complains that it should be there. This is where my code stands (w/o VS complaints):
Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double) MyBase.New(x, y) Me.Z = z End Sub ' Constructor
Is this ok? Thanks, Karen Nooobie to OOP and VB.Net 2005 -
Dave Kreskowiak wrote:
Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal) 'MyBase.New(x, y) ' This line isn't required because you're setting all ' three values in this constructor. Anything you do in ' in the POINT2 constructor would just be overwritten ' by what you have in this one. Me.X = x ' Again, go through the Properties! Me.Y = y Me.Z = z End Sub ' Constructor
Dave, As soon as I comment out the 'Mybase.New()' statement in POINT3 class, VS2005 complains that it should be there. This is where my code stands (w/o VS complaints):
Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double) MyBase.New(x, y) Me.Z = z End Sub ' Constructor
Is this ok? Thanks, Karen Nooobie to OOP and VB.Net 2005There's something you haven't shown in the POINT2.New constructor. This message shouldn't come up with the code that I've seen and what I posted. The code that I posted came straight out of VB.NET 2005 and it compiled nicely. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Dave Kreskowiak wrote:
Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal) 'MyBase.New(x, y) ' This line isn't required because you're setting all ' three values in this constructor. Anything you do in ' in the POINT2 constructor would just be overwritten ' by what you have in this one. Me.X = x ' Again, go through the Properties! Me.Y = y Me.Z = z End Sub ' Constructor
Dave, As soon as I comment out the 'Mybase.New()' statement in POINT3 class, VS2005 complains that it should be there. This is where my code stands (w/o VS complaints):
Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double) MyBase.New(x, y) Me.Z = z End Sub ' Constructor
Is this ok? Thanks, Karen Nooobie to OOP and VB.Net 2005Can you correct this conflict in two methods with similar names but different parameters.
Public Type MyPoint x As Double y As Double End Type Public Type MyLine Head As MyPoint Tail As MyPoint End Type Public Sub DrawLine(aLine As MyLine, Canves As Form) Canves.Line (aLine.Head.x, aLine.Head.y)-(aLine.Tail.x, aLine.Tail.y) End Sub Public Sub DrawLine(aLine As MyLine, Canves As PictureBox) Canves.Line (aLine.Head.x, aLine.Head.y)-(aLine.Tail.x, aLine.Tail.y) End Sub
Shoaib Nawaz
-
Can you correct this conflict in two methods with similar names but different parameters.
Public Type MyPoint x As Double y As Double End Type Public Type MyLine Head As MyPoint Tail As MyPoint End Type Public Sub DrawLine(aLine As MyLine, Canves As Form) Canves.Line (aLine.Head.x, aLine.Head.y)-(aLine.Tail.x, aLine.Tail.y) End Sub Public Sub DrawLine(aLine As MyLine, Canves As PictureBox) Canves.Line (aLine.Head.x, aLine.Head.y)-(aLine.Tail.x, aLine.Tail.y) End Sub
Shoaib Nawaz
What conflict? What's the errot you're getting? I see other things wrong that have nothing to do with Overloading, Shadowing, or Overriding. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome