H
H
Here is a good one!!
A book I am reading on OOP states
'If any method is marked to allow overriding, the access can be changed by a
derived class.'
Also
'A derived class can even change a method's scope to public or private if
it's allowed to overide the method.'
However, if I write this code:
Public MustInherit Class Product
Protected Price As Double = 1
Protected Shipping As Double = 2
Protected Overridable Function CalculatePrice() As Double
CalculatePrice = Price + Shipping
End Function
End Class
Public Class LargeProduct
Inherits Product
Protected ShippingSurcharge As Double = 5
Public Overrides Function CalculatePrice() As Double
CalculatePrice = MyBase.CalculatePrice + ShippingSurcharge
End Function
Public Sub New()
MyBase.New()
End Sub
End Class
I get the error message:
Public Overrides Function CalculatePrice() As Double' cannot override
'Protected Overridable Function CalculatePrice() As Double' because they
have different access levels.
Have I done something wrong or is the book wrong?
Thanks
A book I am reading on OOP states
'If any method is marked to allow overriding, the access can be changed by a
derived class.'
Also
'A derived class can even change a method's scope to public or private if
it's allowed to overide the method.'
However, if I write this code:
Public MustInherit Class Product
Protected Price As Double = 1
Protected Shipping As Double = 2
Protected Overridable Function CalculatePrice() As Double
CalculatePrice = Price + Shipping
End Function
End Class
Public Class LargeProduct
Inherits Product
Protected ShippingSurcharge As Double = 5
Public Overrides Function CalculatePrice() As Double
CalculatePrice = MyBase.CalculatePrice + ShippingSurcharge
End Function
Public Sub New()
MyBase.New()
End Sub
End Class
I get the error message:
Public Overrides Function CalculatePrice() As Double' cannot override
'Protected Overridable Function CalculatePrice() As Double' because they
have different access levels.
Have I done something wrong or is the book wrong?
Thanks