473,888 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hiding a virtual method

I have class A which inherits from class B. B contains the following virtual
method:

public virtual List<TSelect()
{
return new List<T>();
}

Is there any way to make private or otherwise hide this method within A so
that classes instantiating A don't see it?

Nov 27 '06 #1
9 2730
Andrew Robinson wrote:
I have class A which inherits from class B. B contains the following
virtual method:

public virtual List<TSelect()
{
return new List<T>();
}

Is there any way to make private or otherwise hide this method within A
so that classes instantiating A don't see it?
No, once public methods are exposed in your inheritance, it is not
possible to hide them, as this would break polymorphism.

You can declare the method as 'new', but when casting the object to the
superclass, you would still be able to call it.

Best Regards,

Wiebe Tijsma
http://www.e-office.com
Nov 28 '06 #2
This would be a clear violation of the Liskov Substitution Principle with
other words reconsider redesigning your classes.

Why not move the method to class B?

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS******** ******@TK2MSFTN GP06.phx.gbl...
>I have class A which inherits from class B. B contains the following
virtual method:

public virtual List<TSelect()
{
return new List<T>();
}

Is there any way to make private or otherwise hide this method within A so
that classes instantiating A don't see it?

Nov 28 '06 #3
A bit more info:

I am trying to hide the method because I need to create several methods in
class A that call the base method in class B. The original method in B is no
longer valid. I guess I can either leave it alone or override it with a
method that throws an exception (not implemented.)

-A
"Gabriel Lozano-Morán" <ab***@frontbri dge.comwrote in message
news:eT******** ******@TK2MSFTN GP04.phx.gbl...
This would be a clear violation of the Liskov Substitution Principle with
other words reconsider redesigning your classes.

Why not move the method to class B?

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS******** ******@TK2MSFTN GP06.phx.gbl...
>>I have class A which inherits from class B. B contains the following
virtual method:

public virtual List<TSelect()
{
return new List<T>();
}

Is there any way to make private or otherwise hide this method within A
so that classes instantiating A don't see it?

Nov 28 '06 #4
Sorry I mixed up A and B as I thought B was deriving from A. I would say use
the "protected" access modifier but i still believe that we could better
help you if you explain what you are trying to do exactly.

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS******** ******@TK2MSFTN GP04.phx.gbl...
>A bit more info:

I am trying to hide the method because I need to create several methods in
class A that call the base method in class B. The original method in B is
no longer valid. I guess I can either leave it alone or override it with a
method that throws an exception (not implemented.)

-A
"Gabriel Lozano-Morán" <ab***@frontbri dge.comwrote in message
news:eT******** ******@TK2MSFTN GP04.phx.gbl...
>This would be a clear violation of the Liskov Substitution Principle with
other words reconsider redesigning your classes.

Why not move the method to class B?

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS******* *******@TK2MSFT NGP06.phx.gbl.. .
>>>I have class A which inherits from class B. B contains the following
virtual method:

public virtual List<TSelect()
{
return new List<T>();
}

Is there any way to make private or otherwise hide this method within A
so that classes instantiating A don't see it?


Nov 28 '06 #5
Hi Andrew,

If this is simply a versioning issue you should mark method B with the
following attributes:

[Obsolete("The Select method is no longer valid.", true),
EditorBrowsable (System.Compone ntModel.EditorB rowsableState.N ever)]
public virtual List<TSelect()
{
return new List<T>();
}

The Select method will no longer appear in intellisense for members of A or
B and if it's explicitly coded anyway there will be a compiler error
preventing the build.

--
Dave Sexton

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS******** ******@TK2MSFTN GP04.phx.gbl...
>A bit more info:

I am trying to hide the method because I need to create several methods in
class A that call the base method in class B. The original method in B is
no longer valid. I guess I can either leave it alone or override it with a
method that throws an exception (not implemented.)

-A
"Gabriel Lozano-Morán" <ab***@frontbri dge.comwrote in message
news:eT******** ******@TK2MSFTN GP04.phx.gbl...
>This would be a clear violation of the Liskov Substitution Principle with
other words reconsider redesigning your classes.

Why not move the method to class B?

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS******* *******@TK2MSFT NGP06.phx.gbl.. .
>>>I have class A which inherits from class B. B contains the following
virtual method:

public virtual List<TSelect()
{
return new List<T>();
}

Is there any way to make private or otherwise hide this method within A
so that classes instantiating A don't see it?


Nov 28 '06 #6
I am writing a set of data access classes. My base class has a virtual metho
d for each of the CRUD methods. Some derived classes may want to override
these. Pretty standard stuff. But I have now run into a situation where a
derived class needs to implement a couple of different methods for a
particular CRUD method.

base class:
virtual List<TSelect() {}

most derived classes:
override List<MyContaine rSelect() { return base.Select(); }

but a few derived classes will need different flavors of Select:

List<MyContaine rSelectByName() { do something different; return
base.Select(); }
List<MyContaine rSelectByID() { do something else different; return
base.Select(); }

It would be nice to "hide" the base implementation since it might not be
appropriate.

I hear you guys.... and will try to work this from a different angle.

Thanks,
"Gabriel Lozano-Morán" <ab***@frontbri dge.comwrote in message
news:ur******** ******@TK2MSFTN GP04.phx.gbl...
Sorry I mixed up A and B as I thought B was deriving from A. I would say
use the "protected" access modifier but i still believe that we could
better help you if you explain what you are trying to do exactly.

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS******** ******@TK2MSFTN GP04.phx.gbl...
>>A bit more info:

I am trying to hide the method because I need to create several methods
in class A that call the base method in class B. The original method in B
is no longer valid. I guess I can either leave it alone or override it
with a method that throws an exception (not implemented.)

-A
"Gabriel Lozano-Morán" <ab***@frontbri dge.comwrote in message
news:eT******* *******@TK2MSFT NGP04.phx.gbl.. .
>>This would be a clear violation of the Liskov Substitution Principle
with other words reconsider redesigning your classes.

Why not move the method to class B?

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS****** ********@TK2MSF TNGP06.phx.gbl. ..
I have class A which inherits from class B. B contains the following
virtual method:

public virtual List<TSelect()
{
return new List<T>();
}

Is there any way to make private or otherwise hide this method within A
so that classes instantiating A don't see it?


Nov 28 '06 #7
Hi,

In that case you should override the method and throw a
NotSupportedExc eption.

--
Dave Sexton

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:OV******** ******@TK2MSFTN GP06.phx.gbl...
>I am writing a set of data access classes. My base class has a virtual
metho d for each of the CRUD methods. Some derived classes may want to
override these. Pretty standard stuff. But I have now run into a situation
where a derived class needs to implement a couple of different methods for
a particular CRUD method.

base class:
virtual List<TSelect() {}

most derived classes:
override List<MyContaine rSelect() { return base.Select(); }

but a few derived classes will need different flavors of Select:

List<MyContaine rSelectByName() { do something different; return
base.Select(); }
List<MyContaine rSelectByID() { do something else different; return
base.Select(); }

It would be nice to "hide" the base implementation since it might not be
appropriate.

I hear you guys.... and will try to work this from a different angle.

Thanks,
"Gabriel Lozano-Morán" <ab***@frontbri dge.comwrote in message
news:ur******** ******@TK2MSFTN GP04.phx.gbl...
>Sorry I mixed up A and B as I thought B was deriving from A. I would say
use the "protected" access modifier but i still believe that we could
better help you if you explain what you are trying to do exactly.

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS******* *******@TK2MSFT NGP04.phx.gbl.. .
>>>A bit more info:

I am trying to hide the method because I need to create several methods
in class A that call the base method in class B. The original method in
B is no longer valid. I guess I can either leave it alone or override it
with a method that throws an exception (not implemented.)

-A
"Gabriel Lozano-Morán" <ab***@frontbri dge.comwrote in message
news:eT****** ********@TK2MSF TNGP04.phx.gbl. ..
This would be a clear violation of the Liskov Substitution Principle
with other words reconsider redesigning your classes.

Why not move the method to class B?

Gabriel Lozano-Morán

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:eS***** *********@TK2MS FTNGP06.phx.gbl ...
>I have class A which inherits from class B. B contains the following
>virtual method:
>
public virtual List<TSelect()
{
return new List<T>();
}
>
Is there any way to make private or otherwise hide this method within
A so that classes instantiating A don't see it?


Nov 28 '06 #8
Hello Andrew,

I agree with Gabriel and Wiebe that we are not able to "hide" in this
method in class A. But you may condsider "seal" it. A sealed method
overrides a method in a base class, but itself cannot be overridden further
in any derived class. So any classes instantiating A cannot override it and
you can just throws an exception or do something else to let user know this
method is not intend to be used.

Sincerely,

Luke Zhang

Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 28 '06 #9
All good stuff. Thanks everyone.
"Luke Zhang [MSFT]" <lu******@onlin e.microsoft.com wrote in message
news:kN******** ******@TK2MSFTN GXA01.phx.gbl.. .
Hello Andrew,

I agree with Gabriel and Wiebe that we are not able to "hide" in this
method in class A. But you may condsider "seal" it. A sealed method
overrides a method in a base class, but itself cannot be overridden
further
in any derived class. So any classes instantiating A cannot override it
and
you can just throws an exception or do something else to let user know
this
method is not intend to be used.

Sincerely,

Luke Zhang

Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no
rights.
Nov 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4208
by: Lorenzo Villari | last post by:
I premise I don't know C++ well but... I wondered what is this data hiding thing... I mean, if I can look at the header (and i need it beacuse of the class), then what's hidden? Can someone give me an example of something hidden from the user?
7
8676
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
6
538
by: harrylmh | last post by:
Hi, I'm learning C# and I just don't quite understand the need for polymorphism. why do we need to use it? how does a base class variable holding a derived class instance do any good? Also, what's the difference between method hiding and overriding when they're both still overriding the base method. Thanks
4
4159
by: Sharon Tal | last post by:
Hi all. I am trying to figure out the differences between overriding and hiding a method name. The only difference i can see, is that with name hiding i can change the method access level. Are there any other differences? Thanks, Sharon.
17
2927
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
1
1607
by: Tariq | last post by:
Any thoughts on how a method on a base class can be prevented from being overriden or hidden? Basically the idea is that no matter what all derived classes should use the method in my base class. Got to prevent hiding via the new keyword as well.
8
2633
by: SpotNet | last post by:
Hello NewsGroup, I have a base class and six classes that inherit from this base class. All members in the base class are used in it's extended classes except, in one of the extended class one member in the base class is not required. Is it possible to mark a method in the extended class such that it does not appear in that particular extended class? I know you can use the 'new' keyword and redefine the method with a 'private'...
7
3467
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am trying to hide the `complexity` of template from the user interface. If you look at the code DataSet should be the object that my user manipulate. Unfortunately by doing so the object returned by DataSet::Get is a FloatingPt, so without the virtual...
11
3067
by: Aflj | last post by:
This code won't compile (two compilers tried, gcc and VC++, both of recent versions, but I don't remember them exactly): class C1 { public: void M1(int i) {} }; class C2: public C1
0
9959
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11176
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10772
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7144
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5812
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4638
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4241
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.