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

Hiding inherited protected member

Is there a way to hide an inherited protected member so that it is no longer
accessible nor visible from the inherited classes?

The code below makes c1.foo() no longer accessible, but c2.foo is visible to
c3! If instead I make c2.foo private, then c3 will access directly c1.foo

Thanks, Fabio

class c1 {
protected void foo() { MessageBox.Show( "foo: c1" ); }
}

class c2 : c1 {
protected new void foo() { /*stub function */ }
}

class c3 : c2 {
public void zoo() { foo(); }
}
Nov 17 '05 #1
4 2185
Fabio Cannizzo <fc*****************@london.edu> wrote:
Is there a way to hide an inherited protected member so that it is no longer
accessible nor visible from the inherited classes?


No. If a member shouldn't be visible from inherited classes, it
shouldn't be protected.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
I guess that means that if I specialise a class in the hierarchy so that
cartain functionality becomes imposed, and no longer user choice, that is
not possible.
Bless the old C++, which allowed for private inheritance! While I admit it
is exceptional for GUIs, it is so limited for other things. Hope the 2.0
will be better.

Thank you for your reply.

Fabio
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Fabio Cannizzo <fc*****************@london.edu> wrote:
Is there a way to hide an inherited protected member so that it is no
longer
accessible nor visible from the inherited classes?


No. If a member shouldn't be visible from inherited classes, it
shouldn't be protected.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
The problem goes something like this:

If C# were to allow you to override members and make them private in
derived classes, then all the caller has to do is cast the object to
its base class in order to get at the member again. The compiler, not
knowing that this variable holds an upcast instance of a derived class
in which the method or property is private, will allow access to the
member via the vtable, even though it is marked "private" in the
derived class.

I suppose that the run time could throw an exception indicating that
you're trying to access an overridden private member, but then you're
incurring the overhead of run-time access checking.

The best you can do is, as you pointed out, declare the method or
property as "private new". Are you sure that if you declare it "private
new", the derived class (c3) can still get at "foo"? (Of course, it can
always upcast and get at it, but I mean without doing that trick.)

Nov 17 '05 #4
Hi Bruce.

Thaks for your reply.

I have tried the downcasting trick, and that, at least, seems to be
protected. When in c3 I upcast to c1, then c3 is treated just as another
class, and it does not have rights to access private or protected members of
c1.

Fabio

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
The problem goes something like this:

If C# were to allow you to override members and make them private in
derived classes, then all the caller has to do is cast the object to
its base class in order to get at the member again. The compiler, not
knowing that this variable holds an upcast instance of a derived class
in which the method or property is private, will allow access to the
member via the vtable, even though it is marked "private" in the
derived class.

I suppose that the run time could throw an exception indicating that
you're trying to access an overridden private member, but then you're
incurring the overhead of run-time access checking.

The best you can do is, as you pointed out, declare the method or
property as "private new". Are you sure that if you declare it "private
new", the derived class (c3) can still get at "foo"? (Of course, it can
always upcast and get at it, but I mean without doing that trick.)

Nov 17 '05 #5

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

Similar topics

6
by: thechaosengine | last post by:
Hi all, Is there a way to hide a member in a subclass that has been inherited from a base class? Lets leave aside any issues regarding whether its a good idea for a moment. Here's an example...
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
8
by: Allen Anderson | last post by:
Just curious if there is a way to hide a public member inherited from another class. I know you can 'new' to give it your own version, but is there a way to make it no longer public period? I...
6
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
7
by: Dennis | last post by:
I have a class named myclass that inheirits from "baseclass". There is a property of "baseclass" that I don't want exposed in the IDE. The MSDN documentation says" "A derived type can hide an...
6
by: Roka | last post by:
Hi all why cann't I access protected var from a inherited class? Code example: class Base{ protected: int color; public: virtual void show() = 0; };
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
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: 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
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
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
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.