By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 800 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

Overridable Scope

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
As you've learned from your experiment, the book is wrong. You must retain
the same access level when overriding a method, with one exception (which is
noted below).

From the Visual Basic.NET specification (Section 9.2.3):

A method may not override another method if the overriding method's
accessibility domain is not equal to the accessibility domain of the method
being overridden. The one exception is that a method overriding a Protected
Friend method in another assembly must specify Protected (not Protected
Friend).
"H" wrote:
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

Jul 21 '05 #2

P: n/a
H
Thanks

I have read in several book reviews that sams publishing has a tendency to
print inacurate information.

H

"Charlie Williams" <Ha********@nospam.nospam> wrote in message
news:44**********************************@microsof t.com...
As you've learned from your experiment, the book is wrong. You must
retain
the same access level when overriding a method, with one exception (which
is
noted below).

From the Visual Basic.NET specification (Section 9.2.3):

A method may not override another method if the overriding method's
accessibility domain is not equal to the accessibility domain of the
method
being overridden. The one exception is that a method overriding a
Protected
Friend method in another assembly must specify Protected (not Protected
Friend).
"H" wrote:
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

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.