Hiding a virtual method

  • Thread starter Thread starter Andrew Robinson
  • Start date Start date
A

Andrew Robinson

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

public virtual List<T> Select()
{
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?
 
Andrew said:
I have class A which inherits from class B. B contains the following
virtual method:

public virtual List<T> Select()
{
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
 
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
 
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
 
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
 
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.ComponentModel.EditorBrowsableState.Never)]
public virtual List<T> Select()
{
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.
 
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<T> Select() {}

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

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

List<MyContainer> SelectByName() { do something different; return
base.Select(); }
List<MyContainer> SelectByID() { 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,
 
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top