473,474 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Access Modifier of Derived Class Member

2 New Member
Consider Code Sample Below of a simple console application.

Expand|Select|Wrap|Line Numbers
  1. namespace AccessiblityAndMemberClass
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.         }
  8.     }
  9.  
  10.     class MyBaseClass
  11.     {
  12.          protected virtual string GetClassName()
  13.         { 
  14.           return "MyBaseClass";
  15.         }
  16.     }
  17.     class MyDerivedClass : MyBaseClass
  18.     {
  19.         public override string GetClassName()
  20.         {
  21.             return "MyDerivedClass";
  22.         }
  23.     }
  24.  
  25. }
  26.  
  27. Here MyBaseClass is Base class having protected virtual method "GetClassName",now when "MyDerivedClass" class  which inherit from  "MyBaseClass"  try to override this method by widening access modifier  code doesn't compile 
  28.  
  29.   This Scenario explains in derived class can't widen restrictive access modifier  if specified in Base class.
  30.  
  31. Now consider one more code sample
  32.  
  33.    class MyBaseClass1
  34.     {
  35.          public virtual string GetClassName()
  36.         {
  37.             return "MyBaseClass";
  38.         }
  39.     }
  40.     class MyDerivedClass1 : MyBaseClass1
  41.     {
  42.          override string GetClassName()
  43.         {
  44.             return "MyDerivedClass";
  45.         }
  46.     }
Here I tried to restrict access modifiers in derived class for method GetClassName that one is also not allowed.

Moral of the story is in csharp keep access modifier of derived class member same as base class if member is virtual method/function.
Mar 12 '12 #1
0 3787

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method...
6
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
14
by: Edward Diener | last post by:
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...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
2
by: kaferro | last post by:
I have a base class that has about ten variables. Later, I have a derived class that adds five more variables. In the derived class' setData() function, I pass an instance of the base class, and...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
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...
11
by: Eran.Yasso | last post by:
Hi All, this issue curious me for long time. Any one knows why Microsoft didn't allow to different access modifier for set and get? To be more specific, why didn't they allow us to do it like...
6
by: chris.kemmerer | last post by:
I am having a problem with templates and I hope someone here can help. I am writing a library that accepts data packets, parses them and saves the information for later use. One member of the...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
0
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,...
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
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,...
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,...
1
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.