473,544 Members | 1,810 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 24547

"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
4324
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...
3
3065
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 ...
3
1766
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
1836
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
1689
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
2822
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
3283
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
1298
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...
0
1021
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
7416
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7360
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...
0
7600
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. ...
0
7761
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...
1
7363
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5899
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5289
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3403
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...
1
1833
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

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.