Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide a base class public property in the derived class

Tags:

c#

I want to hide the base public property(a data member) in my derived class:

class Program {     static void Main(string[] args)     {         b obj = new b();         obj.item1 = 4;// should show an error but it doent ???     } }  class a {     public int item1 {get; set;}     public int item2 { get; set; } }  class b : a {     new private int item1; }  class c : a {  } 

i have member as public because i want the member to be inherited in c class , but want to hide the member in b class , how can i do this ?

dont i have an option to selectively inherite the variable i want in my base class ??? thats really bad , i think ms should provide us with an option (may be a modifier) to perform this


Edit:

I found the answer myself (i heard lots of them telling this is not possible in c#, but you can kind of do it)

I am including the code in case it is useful

class Program {     static void Main(string[] args)     {         b obj = new b();         obj.item1 = 4; // shows an error  : )     } }  class a {     public int item1 { get; set; }     public int item2 { get; set; } }  class b : a {     new public static int item1     {         get;         private set;     } } 
like image 534
ravikiran Avatar asked Sep 18 '09 11:09

ravikiran


People also ask

How do you hide base class methods?

It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.

Can you cast a base class to a derived class?

Likewise, a reference to base class can be converted to a reference to derived class using static_cast . If the source type is polymorphic, dynamic_cast can be used to perform a base to derived conversion.

Can the private members of base class be used in the derived class?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them.

How do you access base class members in a derived class?

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. Constructors, destructors and copy constructors of the base class.


2 Answers

I'm going to attempt to explain with examples why this is a bad idea, rather than using cryptic terms.

Your proposal would be to have code that looks like this:

public class Base {     public int Item1 { get; set; }     public int Item2 { get; set; } }   public class WithHidden : Base {     hide Item1; // Assuming some new feature "hide" in C# }  public class WithoutHidden : Base { } 

This would then make the following code invalid:

WithHidden a = new WithHidden(); a.Item1 = 10; // Invalid - cannot access property Item1 int i = a.Item1; // Invalid - cannot access property Item1 

And that would be just what you wanted. However, suppose we now have the following code:

Base withHidden = new WithHidden(); Base withoutHidden = new WithoutHidden();  SetItem1(withHidden); SetItem1(withoutHidden);  public void SetItem1(Base base) {     base.Item1 = 10; } 

The compiler doesn't know what runtime type the argument base in SetItem1 will be, only that it is at least of type Base (or some type derived from Base, but it can't tell which -- it may be obvious looking at the code snippet, but more complex scenarios make it practically impossible).

So the compiler will not, in a large percentage of the cases, be able to give a compiler error that Item1 is in fact inaccessible. So that leaves the possibility of a runtime check. When you try and set Item1 on an object which is in fact of type WithHidden it would throw an exception.

Now accessing any member, any property on any non-sealed class (which is most of them) may throw an exception because it was actually a derived class which hid the member. Any library which exposes any non-sealed types would have to write defensive code when accessing any member just because someone may have hidden it.

A potential solution to this is to write the feature such that only members which declare themselves hideable can be hidden. The compiler would then disallow any access to the hidden member on variables of that type (compile time), and also include runtime checks so that a FieldAccessException is thrown if it is cast to the base type and tried to be accessed from that (runtime).

But even if the C# developers did go to the huge trouble and expense of this feature (remember, features are expensive, especially in language design) defensive code still has to be written to avoid the problems of potential FieldAccessExceptions being thrown, so what advantage over reorganising your inheritance hierarchy have you gained? With the new member hiding feature there would be a huge number of potential places for bugs to creep into your application and libraries, increasing development and testing time.

like image 77
ICR Avatar answered Oct 02 '22 13:10

ICR


What you want to do goes directly against the grain of OO, you can't 'unpublish' members as this violates the substitution principle. You have to refactor this into something else.

like image 21
Henk Holterman Avatar answered Oct 02 '22 14:10

Henk Holterman