473,465 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Changing access modifier levels in a derived class

In C++ one can change the access level in a derived class by the "using"
keyword. So, for instance, a public method in a base class can be made
private in a derived class. Is there any way of doing this in C# ?
Nov 16 '05 #1
14 24520

"Edward Diener" <di***************@ORsoftware.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
In C++ one can change the access level in a derived class by the "using"
keyword. So, for instance, a public method in a base class can be made
private in a derived class. Is there any way of doing this in C# ?


No, C# does not allow for the changing of accessibility of methods(not sure
about IL, however).

Out of curiosity, what kind of scenarios would this allow? Does it have a
use outside of maneveruing around badly designed base classes, which is the
only thing I can think of off hand?
Nov 16 '05 #2
Not true.

C# does allow you to change accessibility of base class items in a
derived class, but with caveats.

First, (perhaps obviously) you can widen access. You can override a
protected base class item and declare the override public in the
derived class.

Second, (and I think this is what Edward was asking), you can use the
"new" keyword to indicate that your derived class has a new
implementation for a base class item. The "new" derived item can have
any access at all, including (usually) narrower access. So if you have
a protected method in your base class, you can declare that same method
with the "new" keyword in your derived class and make it private.

However, it's not that simple.

Because the "new" derived class method is not an override (it's a
redefinition), if someone casts your derived class object to its base
class, then the base class method will be called instead of the derived
class method. Remember: the "new" derived class method is not an
override and does not create polymorphic behaviour. For example:

class A
{
public virtual int Add(int a, int b)
{ ... }
}

class B : A
{
public new int Add(int a, int b)
{ ... }
}

B aB = new B();
int x = aB.Add(1, 2);
A anA = aB;
int y = anA.Add(1, 2);

In this example, the first call to aB.Add() would call B.Add. The
second call, anA.Add, would call A.Add(). If B.Add had been declared
"public override" instead of "public new", both calls would have
invoked B.Add().

You can, of course, declare B.Add "private new" or give it whatever
access level you like.

Nov 16 '05 #3

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Not true.

C# does allow you to change accessibility of base class items in a
derived class, but with caveats.

First, (perhaps obviously) you can widen access. You can override a
protected base class item and declare the override public in the
derived class.


I've love to see an example of this. I just tried it and got this error:

'Test1.Test.TestMethod()': cannot change access modifiers when overriding
'protected' inherited member 'Test.Program.TestMethod()'
Nov 16 '05 #4
This compiles, but there are many reasons to avoid it.

public class A
{
protected void Foo()
{
Console.WriteLine("Foo");
}
}

public class A : B
{
public new void Foo()
{
base.Foo();
}
}

Regards,
Joakim

Daniel O'Connell [C# MVP] wrote:
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Not true.

C# does allow you to change accessibility of base class items in a
derived class, but with caveats.

First, (perhaps obviously) you can widen access. You can override a
protected base class item and declare the override public in the
derived class.

I've love to see an example of this. I just tried it and got this error:

'Test1.Test.TestMethod()': cannot change access modifiers when overriding
'protected' inherited member 'Test.Program.TestMethod()'

Nov 16 '05 #5
Of course that should be

public class A
{
protected void Foo()
{
Console.WriteLine("Foo");
}
}

public class B : A
{
public new void Foo()
{
base.Foo();
}
}

Regards,
Joakim

Joakim Karlsson wrote:
This compiles, but there are many reasons to avoid it.

public class A
{
protected void Foo()
{
Console.WriteLine("Foo");
}
}

public class A : B
{
public new void Foo()
{
base.Foo();
}
}

Regards,
Joakim

Daniel O'Connell [C# MVP] wrote:
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Not true.

C# does allow you to change accessibility of base class items in a
derived class, but with caveats.

First, (perhaps obviously) you can widen access. You can override a
protected base class item and declare the override public in the
derived class.

I've love to see an example of this. I just tried it and got this error:

'Test1.Test.TestMethod()': cannot change access modifiers when
overriding 'protected' inherited member 'Test.Program.TestMethod()'

Nov 16 '05 #6
...and of course I should have read the entrire thread before jumping in
like this. I see you guys have already covered this.

My bad.

Regards,
Joakim

Joakim Karlsson wrote:
Of course that should be

Nov 16 '05 #7
You are absolutely correct. My bad. An overridden item must have
exactly the same signature as the base class item.

In order to narrow _or widen_ the access to an item, you must declare
it "new", and in so doing you give up polymorphism.

Again, my apologies: I was confusing C# with Java, which does allow
increasing the visibility of an overridden member in the subclass. Does
anyone know why C# doesn't allow this?

Nov 16 '05 #8
> Again, my apologies: I was confusing C# with Java, which does allow
increasing the visibility of an overridden member in the subclass. Does
anyone know why C# doesn't allow this?

I've no idea. My best guess would be that they wanted to avoid exposing a
method accidentally by typing the wrong accessibility modifier(public
override instaed of protected override). Personally I don't think its much
of a loss, new can be used or an entirely new method defined.

Assuming the runtime permits this anyway, I've yet to work up the energy to
pour through the spec and determine if its a spec feature or not(Although I
suspect the spec permits it, since explicit interface implementations are
not public).
Nov 16 '05 #9
Bruce Wood <br*******@canada.com> wrote:
Not true.

C# does allow you to change accessibility of base class items in a
derived class, but with caveats.

First, (perhaps obviously) you can widen access. You can override a
protected base class item and declare the override public in the
derived class.
No you can't. You can in Java, but not in C#. If you try it, you get an
error message such as:

Test.cs(11,26): error CS0507: 'Derived.Foo()': cannot change access
modifiers when overriding 'protected' inherited member 'Base.Foo()'
Second, (and I think this is what Edward was asking), you can use the
"new" keyword to indicate that your derived class has a new
implementation for a base class item. The "new" derived item can have
any access at all, including (usually) narrower access. So if you have
a protected method in your base class, you can declare that same method
with the "new" keyword in your derived class and make it private.


That's not changing the accessibility of a base class item though. It's
introducing a new item with the same name, as you then say.

It's a shame that you can't widen access in C# as you can in Java, but
I'm glad you can't narrow it as that would break Liskov's
substitutability rule (depending on what it actually meant to narrow
the access, of course).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...

"Edward Diener" <di***************@ORsoftware.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
In C++ one can change the access level in a derived class by the "using"
keyword. So, for instance, a public method in a base class can be made
private in a derived class. Is there any way of doing this in C# ?


No, C# does not allow for the changing of accessibility of methods(not
sure about IL, however).

Out of curiosity, what kind of scenarios would this allow? Does it have a
use outside of maneveruing around badly designed base classes, which is
the only thing I can think of off hand?


Deriving my own class from a .NET framework base class I might wish to
remove some functionality of that base class from the end-programmer of my
derived class. This seems to me a fairly common possibility.
Nov 16 '05 #11

"Edward Diener" <di***************@ORsoftware.com> wrote in message
news:eu**************@TK2MSFTNGP10.phx.gbl...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...

"Edward Diener" <di***************@ORsoftware.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
In C++ one can change the access level in a derived class by the "using"
keyword. So, for instance, a public method in a base class can be made
private in a derived class. Is there any way of doing this in C# ?


No, C# does not allow for the changing of accessibility of methods(not
sure about IL, however).

Out of curiosity, what kind of scenarios would this allow? Does it have a
use outside of maneveruing around badly designed base classes, which is
the only thing I can think of off hand?


Deriving my own class from a .NET framework base class I might wish to
remove some functionality of that base class from the end-programmer of my
derived class. This seems to me a fairly common possibility.


Hmm, I suppose it would have some value, although I would question the
point. The end user could cast down to the base object and make the call
anyway, and doing so does violate the oft-touted Liskov principal.

Have you considered overriding it and applying one of the non browse
attributes(don't know 'em off hand, sorry) so it won't show up in
intellisense?
Nov 16 '05 #12
I have run across this problem a couple of times in C#. For example, I
wrote an XmlFragmentReader, which is based on XmlTextReader. It's a
very close fit, but not quite: some methods of XmlTextReader don't make
any sense in the context of the derived class. I settled for returning
null or doing nothing if they're called, but it would have been nicer
to be able to hide them in some way other than using "new".

I agree, however, that it's unclear what would be the semantics of
narrowing access via an override. It's a shame that you can't widen
access á la Java, though: I've also found some situations in which
that would be handy.

Nov 16 '05 #13
(hmm, don't know why but OE didn't comment this on reply...I really need to
find a new newsreader)

I do think that widening accessbility would be nice(although I don't think
it should be implicit by any means), but narrowing still bothers me.

In your example, either it was the wrong base class or the base class isn't
granular enough. Unfortuantly with the framework we are stuck with what
there already is. Hiding the method still would not achieve anything better
than new would...the same method could be called from a base class
reference.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have run across this problem a couple of times in C#. For example, I
wrote an XmlFragmentReader, which is based on XmlTextReader. It's a
very close fit, but not quite: some methods of XmlTextReader don't make
any sense in the context of the derived class. I settled for returning
null or doing nothing if they're called, but it would have been nicer
to be able to hide them in some way other than using "new".

I agree, however, that it's unclear what would be the semantics of
narrowing access via an override. It's a shame that you can't widen
access á la Java, though: I've also found some situations in which
that would be handy.
Nov 16 '05 #14
Yes, that's what I meant by "unclear semantics". If you required that a
method with narrowed access really be hidden--even when the object is
cast to a base class--then you break compile-time type checking. That's
why they added the "new" keyword to the language. I can understand not
allowing access narrowing because of this.

Widening accessibility would be nice, though, and doesn't come with the
same semantic difficulties.

Nov 16 '05 #15

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

Similar topics

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...
3
by: Pete | last post by:
Hello Can anyone provide advice/links on somewhere to get a definitive explanation with Examples of how "protected internal" differs from "protected", "internal" and the other access modifiers ...
3
by: Nathan Wiegman | last post by:
Hi, Why must derived classes access protected base members through an instance of a derived class? The following code would work in C++, why doesn't it in C#? public class A { protected...
10
by: Peter Aitken | last post by:
Supposedly you are supposed to use new when you want a member in a derived class to hide a member of the same name in the base class: class A { public int total; } class B:A {
6
by: Edward Diener | last post by:
Since a C++ using declaration isn't allowed in MC++, is there a way to specify that a property, method, event, or field's access can be changed in a derived class, ie. is protected in one class and...
13
by: dragoncoder | last post by:
Consider the following code #include <iostream> class Base { public: virtual void say() { std::cout << "Base" << std::endl; } }; class Derived: public base {
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
1
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I want to confirm my understanding about access modifier for property. Assembly One contains: Class One has Protected property Protected1_prop Class Two has Friend property Friend2_prop Class...
0
by: srividyam03 | last post by:
hi can u please tell me ... can you narrow the access modifier of derived class while overriding the method which is in base class for example , can you change the public method in base class 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...
1
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.