473,396 Members | 1,840 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.

Overridable Scope

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
2 2699
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marina | last post by:
Hi, I have a class that inherits from CollectionBase, and I'm trying to override its Count property. As per the documentation, this property is overridable - so I should be able to do this. So...
1
by: bminder | last post by:
In the asp.net pages below, Common.vb has an overridable Page_Load sub. In the consuming page, Two.aspx, the Page_Load sub is inherited, but for some reason it (Overrides Sub Page_Load) executes...
1
by: steve kain | last post by:
Hi. I'm just wondering how come the compiler complains when you declare a new sub as overridable but it then lets you define multiple new subs with different interfaces, essentially overriding...
3
by: dbuchanan | last post by:
Why can't I add the 'Overridable' key word to any of the methods or events in my base class? VB.NET thinks I am trying to define a variable. What am I doing wrong? I get the squiggly line under...
2
by: H | last post by:
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...
2
by: MDFS | last post by:
Hi, I was wondering if anyone has any information on the performance impact of using functions declared as overridable? To Explain more fully; By default every function created in VB is NOT...
3
by: grawsha2000 | last post by:
Hello, I have an overridable function in a base class (to do some basic database works) that gets inherited by many concrete classes. These concrete classes expands the work of that...
5
by: Ben | last post by:
Hi, i defined a function in the base class 'ford' and the same function (with different output) in subclass "peugeot". I first put 'Overridable function' in the base class and 'Overrides...
0
by: Internet User | last post by:
According to MSDN, You should never base the security of your application on a member that is marked with the internal virtual modifier in C# (the Overloads Overridable Friend modifier in Visual...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.