CSharp/C# Tutorial - C# Inherited Members






Hiding Inherited Members

A base class and a subclass may define identical members. For example:


public class A { 
   public int Counter = 1; 
} 

public class B : A { 
   public int Counter = 2; 
} 

The Counter field in class B is said to hide the Counter field in class A.

To hide a member deliberately, in which case you can apply the new modifier to the member in the subclass.

The new modifier does nothing more than suppress the compiler warning that would otherwise result:


public class A { 
   public int Counter = 1; 
} 
public class B : A { 
   public new int Counter = 2; 
} 

The new modifier tells the compiler that the duplicate member is not an accident.





new versus override

Consider the following class hierarchy:


public class BaseClass {
    public virtual void Foo() { Console.WriteLine ("BaseClass.Foo"); } 
} 
public class Overrider : BaseClass {
    public override void Foo() { Console.WriteLine ("Overrider.Foo"); } 
} 
public class Hider : BaseClass {
    public new void Foo() { Console.WriteLine ("Hider.Foo"); } 
} 

The differences in behavior between Overrider and Hider are demonstrated in the following code:


Overrider over = new Overrider(); 
BaseClass b1 = over; /*from   w  w  w  . ja  va 2s  . c  o m*/
over.Foo(); // Overrider.Foo 
b1.Foo();   // Overrider.Foo 


Hider h = new Hider(); 
BaseClass b2 = h; 
h.Foo();  // Hider.Foo 
b2.Foo(); // BaseClass.Foo 





Sealing Functions and Classes

An overridden function member may seal its implementation with the sealed keyword to prevent it from being overridden.


public sealed override decimal FinalPrice { 
   get { 
      return Price; 
   }
} 

You can also seal the class itself, implicitly sealing all the virtual functions, by applying the sealed modifier to the class itself.

The base Keyword

The base keyword is similar to the this keyword.

It serves two essential purposes:

  • Accessing an overridden function member from the subclass
  • Calling a base-class constructor

public class DiscountProduct : Item {
     ...
     public override decimal FinalPrice {
         get { 
            return base.FinalPrice + Price; 
         }
     } 
} 

With the base keyword, we access Item's FinalPrice property nonvirtually.

Constructors and Inheritance

A subclass must declare its own constructors.

The base class's constructors are accessible to the derived class, but are not automatically inherited.

For example, if we define Baseclass and Subclass as follows:


public class Baseclass {
   public int X;
   public Baseclass () { }
   public Baseclass (int x) { this.X = x; } 
} 

public class Subclass : Baseclass { } 

the following is illegal:


Subclass s = new Subclass (123); 

Subclass must redefine any constructors it wants to expose.

By doing so, it can call any of the base class's constructors with the base keyword:


public class Subclass : Baseclass {
    public Subclass (int x) : base (x) { } 
} 

The base keyword works like the this keyword, except that it calls a constructor in the base class.

If a constructor in a subclass omits the base keyword, the base type's parameterless constructor is implicitly called:

public class BaseClass { 
   public int X; 
   public BaseClass() { X = 1; } 
} 

public class Subclass : BaseClass { 
   public Subclass() { Console.WriteLine (X); } // 1 
} 

If the base class has no accessible parameterless constructor, subclasses must use the base keyword in their constructors.