473,707 Members | 2,285 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 24596

"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
4341
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
3080
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
1775
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
1856
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
1700
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
2840
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
3303
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
1305
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
1035
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
8787
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9158
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...
1
9060
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6615
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5939
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4454
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
4712
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2508
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2096
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.