I am having difficulty in understanding how the below code is working.
The goal is to have two abstract classes A and B. Then a variable declared of type A, will call the corresponding implementation of GetCost method, of any concrete class that inherits B (in this example C).
In Main, I have created an instance of C and assigned it to variable x, of type A. Every time I run this code, the value I get is 1, instead of 3.
If possible, I would like to keep both the methods abstract in class A and class B and just implement in C. I want to use abstract classes and not an interface.
Any help/guidance will be appreciated.
Module Module1
Sub Main()
Dim x As A = New C
Console.WriteLine(x.Getcost())
Console.ReadLine()
End Sub
Public MustInherit Class A
Public Overridable Function Getcost() As Decimal
Return 1
End Function
End Class
Public MustInherit Class B
Inherits A
Public MustOverride Shadows Function Getcost() As Decimal
End Class
Public Class C
Inherits B
Public Overrides Function GetCost() As Decimal
Return 3
End Function
End Class
End Module
The goal is to have two abstract classes A and B. Then a variable declared of type A, will call the corresponding implementation of GetCost method, of any concrete class that inherits B (in this example C).
In Main, I have created an instance of C and assigned it to variable x, of type A. Every time I run this code, the value I get is 1, instead of 3.
If possible, I would like to keep both the methods abstract in class A and class B and just implement in C. I want to use abstract classes and not an interface.
Any help/guidance will be appreciated.
Module Module1
Sub Main()
Dim x As A = New C
Console.WriteLine(x.Getcost())
Console.ReadLine()
End Sub
Public MustInherit Class A
Public Overridable Function Getcost() As Decimal
Return 1
End Function
End Class
Public MustInherit Class B
Inherits A
Public MustOverride Shadows Function Getcost() As Decimal
End Class
Public Class C
Inherits B
Public Overrides Function GetCost() As Decimal
Return 3
End Function
End Class
End Module