473,398 Members | 2,404 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,398 software developers and data experts.

Hiding a base function so it is not accessible

According to the MSDN (see reference below), it is possible to change
a base class function from public to private. How is this done? I've
tried using "new", but the function is still accessible.

From MSDN:
"A derived type can hide an inherited member by defining a new member
with the same signature. This might be done to make a previously
public member private or to define new behavior for an inherited
method that is marked as final."

http://msdn2.microsoft.com/en-us/lib...t6(VS.71).aspx
Dec 12 '07 #1
7 1654
On Dec 12, 7:15 am, Mike <MLM...@hotmail.comwrote:
According to the MSDN (see reference below), it is possible to change
a base class function from public to private. How is this done? I've
tried using "new", but the function is still accessible.

From MSDN:
"A derived type can hide an inherited member by defining a new member
with the same signature. This might be done to make a previously
public member private or to define new behavior for an inherited
method that is marked as final."

http://msdn2.microsoft.com/en-us/lib...t6(VS.71).aspx
Base:
public void DoSomething()
{}

Derived:
private new string DoSomething()
{ return ""; }

its either that or new private string -
The term as I know its used in VB is shadowing methods
Dec 12 '07 #2
Using "new" does not do it. Perhaps it appears to work for you because
you also changed the return type from void to string?

On Dec 12, 8:27 am, "cfps.Christian" <ge0193...@otc.eduwrote:
On Dec 12, 7:15 am, Mike <MLM...@hotmail.comwrote:
According to the MSDN (see reference below), it is possible to change
a base class function from public to private. How is this done? I've
tried using "new", but the function is still accessible.
From MSDN:
"A derived type can hide an inherited member by defining a new member
with the same signature. This might be done to make a previously
public member private or to define new behavior for an inherited
method that is marked as final."
http://msdn2.microsoft.com/en-us/lib...t6(VS.71).aspx

Base:
public void DoSomething()
{}

Derived:
private new string DoSomething()
{ return ""; }

its either that or new private string -
The term as I know its used in VB is shadowing methods
Dec 12 '07 #3
Mike wrote:
Thanks Jon, but why does the MSDN say it's possible?

Also, I would tend to disagree with the "don't derive from it"
approach. I can't see the benefit in creating a whole new class that
is 99% redundant code. There is a function in the base class that has
parameters. My derived class does not need the parameters because it
always knows what the values should be. I want to replace that
function with one that takes no parameters. Is there another approach
I have overlooked?
You don't need to get rid of the old method then - simply oveload it
with your new method.

As for the philosophical discussion, if you were able to remove methods
from classes that you inherited from then .NET wouldn't be able to
support polymorphism... which is a pretty big price to pay for the
fairly rare case in which you'd want to do what you were suggesting.

Best Regards,

James Crosswell
Microforge.net LLC
http://www.microforge.net
Dec 12 '07 #4
On Dec 12, 2:20 pm, Mike <MLM...@hotmail.comwrote:
Thanks Jon, but why does the MSDN say it's possible?
Basically it's badly written.
Also, I would tend to disagree with the "don't derive from it"
approach. I can't see the benefit in creating a whole new class that
is 99% redundant code. There is a function in the base class that has
parameters. My derived class does not need the parameters because it
always knows what the values should be. I want to replace that
function with one that takes no parameters. Is there another approach
I have overlooked?
Create a parameterless overload of the method which then calls the one
that takes parameters.

You should ask yourself what should happen if someone calls the method
with a *different* set of parameters though. If you can't handle that,
then you shouldn't be deriving from the class.

Note that you don't always need to reimplement the same functionality
with redundant code, however - use aggregation instead of derivation,
i.e. create an instance of the other class "inside" your class, rather
than deriving from it. Sometimes that won't work, but often it will.

Jon
Dec 12 '07 #5
Thanks for the info. Yes, overloading is needed but I never want the
old method called outside my class because passing the wrong values
will cause problems. I am not really talking about removing the
function, just changing it from public to private. For now, I have
overridden the function I want to hide so it throws an exception when
called. Not pretty, but it keeps anyone from using it.

Thanks.

On Dec 12, 10:35 am, James Crosswell <ja...@microforge.netwrote:
Mike wrote:
Thanks Jon, but why does the MSDN say it's possible?
Also, I would tend to disagree with the "don't derive from it"
approach. I can't see the benefit in creating a whole new class that
is 99% redundant code. There is a function in the base class that has
parameters. My derived class does not need the parameters because it
always knows what the values should be. I want to replace that
function with one that takes no parameters. Is there another approach
I have overlooked?

You don't need to get rid of the old method then - simply oveload it
with your new method.

As for the philosophical discussion, if you were able to remove methods
from classes that you inherited from then .NET wouldn't be able to
support polymorphism... which is a pretty big price to pay for the
fairly rare case in which you'd want to do what you were suggesting.

Best Regards,

James Crosswell
Microforge.net LLChttp://www.microforge.net
Dec 12 '07 #6
Also, I would tend to disagree with the "don't derive from it"
approach. I can't see the benefit in creating a whole new class that
is 99% redundant code. There is a function in the base class that has
parameters. My derived class does not need the parameters because it
always knows what the values should be. I want to replace that
function with one that takes no parameters. Is there another approach
I have overlooked?
As others have said, you can simply overload the method. There typically
isn't a problem with users of your class calling the base method (if they
know the parameters). If you absolutely don't want users of your class to
call the base method, override that method and have it either call the
method w/o parameters or throw an exception. You can also add the "Obsolete"
attribute to the overridden method so that users of your class will get a
compiler warning if they try to use it.

Example:

[Obsolete("Use the generic NewObject<Tcall for maximum efficiency.")]
public PersistentObject NewObject(Type objectType, object aKey)

Dec 12 '07 #7
"Mike" <ML****@hotmail.comschrieb
Ahh, but you don't inherit everything. If I have a constructor in my
base class that takes parameters, my derived class is not required
to have a constructor with the same parameters. The derived class
can
have a constructor with no parameters while passing hard coded
values to the base constructor. That is basically what I want to do
with my member function.
No, constructors are not inherited because constructors determine how you
can create an instance of the class.
Armin

Dec 12 '07 #8

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

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
6
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,...
17
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...
9
by: bob | last post by:
Hi, I know there exists a good reason why the designers of c++ decided that function hiding should exist. But I don't know why. Can anybody provide a good reason/example of a case where function...
3
by: Nicolas Castagne | last post by:
Hi all, I have been wondering for a while why function hiding (in a derived class) exists in C++, e.g. why when writing class Base { void foo( int ) {} }; class Derived: public Base { void...
2
by: Frederick Gotham | last post by:
Is there any way to prevent hiding of a base class function? I want the derived class to add an overload of the function, rather than hide the base class one. The following snippet demonstrates...
5
by: ajk | last post by:
Hi I was wondering how one normally does to hide standard properties in a user control e.g. X,Y or Width. I have one approach where I have a base object which hides some of the properties and...
2
by: developer.new | last post by:
Hi I have a question regarding this concept I learned about recently: Name Hiding. Here's what I've come across: There is a base class with two functions with the same name but different...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.