base and name hiding

In this chapter you will learn:

  1. A demo showing what is name hiding
  2. How to use base to reference parent class
  3. How to call a hiddel method from parent class

What is name hiding

An example of inheritance-related name hiding.

using System; //j  av a  2s. c om
 
class BaseClass { 
  public int i = 0; 
} 
 
// Create a derived class. 
class DerivedClass : BaseClass { 
  new int i; // this i hides the i in BaseClass 
 
  public DerivedClass(int b) { 
    i = b; // i in DerivedClass 
  } 
 
  public void show() { 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    DerivedClass ob = new DerivedClass(2); 
 
    ob.show(); 
  } 
}

The code above generates the following result.

Use base to reference member from parent class

The following code shows to how to use base to overcome name hiding.

using System; //  j a v a 2s. c  om
 
class BaseClass { 
  public int i = 0; 
} 
 
// Create a derived class. 
class DerivedClass : BaseClass { 
  new int i; // this i hides the i in BaseClass 
 
  public DerivedClass(int a, int b) { 
    base.i = a; // this uncovers the i in BaseClass 
    i = b; // i in DerivedClass 
  } 
 
  public void show() { 
    // this displays the i in BaseClass. 
    Console.WriteLine("i in base class: " + base.i); 
 
    // this displays the i in DerivedClass 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    DerivedClass ob = new DerivedClass(1, 2); 
 
    ob.show(); 
  } 
}

The code above generates the following result.

Call a hidden method from parent class

using System; //from j a  va 2s.c  o  m
 
class BaseClass { 
  public int i = 0; 
 
  // show() in BaseClass 
  public void show() { 
    Console.WriteLine("i in base class: " + i); 
  } 
} 
 
// Create a derived class. 
class DerivedClass : BaseClass { 
  new int i; // this i hides the i in BaseClass 
 
  public DerivedClass(int a, int b) { 
    base.i = a; // this uncovers the i in BaseClass 
    i = b; // i in DerivedClass 
  } 

  // This hides show() in BaseClass. Notice the use of new. 
  new public void show() { 
    base.show(); // this calls show() in BaseClass 
 
    // this displays the i in DerivedClass 
    Console.WriteLine("i in derived class: " + i); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    DerivedClass ob = new DerivedClass(1, 2); 
 
    ob.show(); 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Get to know C#'s Access Specifiers
  2. Matrix showing the usage of access modifiers
  3. A demo showing how to use access modifiers
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances