473,595 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ 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 24565

"Edward Diener" <di************ ***@ORsoftware. com> wrote in message
news:O8******** ******@TK2MSFTN GP12.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*******@cana da.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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.Tes tMethod()': cannot change access modifiers when overriding
'protected' inherited member 'Test.Program.T estMethod()'
Nov 16 '05 #4
This compiles, but there are many reasons to avoid it.

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

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

Regards,
Joakim

Daniel O'Connell [C# MVP] wrote:
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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.Tes tMethod()': cannot change access modifiers when overriding
'protected' inherited member 'Test.Program.T estMethod()'

Nov 16 '05 #5
Of course that should be

public class A
{
protected void Foo()
{
Console.WriteLi ne("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.WriteLi ne("Foo");
}
}

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

Regards,
Joakim

Daniel O'Connell [C# MVP] wrote:
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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.Tes tMethod()': cannot change access modifiers when
overriding 'protected' inherited member 'Test.Program.T estMethod()'

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*******@cana da.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
substitutabilit y rule (depending on what it actually meant to narrow
the access, of course).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

1
4332
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 inherited base member's access level in the derived class: its access level in the base class and the type of inheritance (public, protected, or private). After this determination is made, the following possibilities exist for manually changing the...
3
3072
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 For "protected internal" some places appear to say it means a union of protected AND internal, other places say it's protected OR internal Thanks Pet
3
1771
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 int x; }
10
1847
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
1694
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 is made public in a derived class ?
13
2831
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
3289
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 signed/unsigned modifier to class declarations to next revision of C++ programming language
1
1299
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 Three has Protected Friend property Protected_Friend3_prop Class Four has Private property Private4_prop Class Derived_One is a class derived from Class One of Assembly One Class Derived_Two is a class derived from Class Two of Assembly One
0
1027
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 protected in derived class while overriding narrowing is possible or not ??? widening is possible or not ?? please help me
0
7883
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
8262
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
8379
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
8252
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3875
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...
0
3915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
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
1
1491
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1226
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.