What is variable Hiding? When you have a variable with the same name in both a base class and a derived class which value is hiding?
In C#, when you have a variable with the same name in both a base class and a derived class, this concept is known as variable hiding (not to be confused with method overriding).
When you access that variable from an instance of the derived class, the variable from the derived class hides the one from the base class.
That's why the value from the base class might not be visible.
Let's break down how this works and why you see only the value from the child class.
Example Code: V
ariable Hiding in C#
using System;
public class BaseClass
{
public string Name = "BaseClass";
public void ShowBaseName()
{
Console.WriteLine($"Base class Name: {Name}");
}
}
public class ChildClass : BaseClass
{
public string Name = "ChildClass";
public void ShowChildName()
{
Console.WriteLine($"Child class Name: {Name}");
}
}
public class Program
{
public static void Main()
{
// Creating an instance of the child class
ChildClass child = new ChildClass();
// Accessing the Name variable directly
Console.WriteLine(child.Name);
// Outputs: ChildClass
// Accessing the method from base class
child.ShowBaseName();
// Outputs: Base class Name: ChildClass
}
}
Explanation
V
ariable Hiding:
The ChildClass has a variable named Name that hides the Name variable from BaseClass.
When you access child.Name, it refers to Name in ChildClass.
Base Class Method Access:
Even when you call ShowBaseName() (a method from the base class), it uses the Name variable of the derived class because it refers to the instance of the derived class.
Why the Base Class Value is Not Showing
When you access the variable Name, the derived class's variable hides the base class's variable. Even though the ShowBaseName() method belongs to the base class, it uses the Name property from the instance that called it, which in this case is of type ChildClass.
How to Access the Base Class Variable
If you need to access the base class variable explicitly, you can use the base keyword (if you’re inside a method in the derived class) or type casting.
Solution 1: Using base Keyword
Modify ShowChildName() method to access the base class variable:
csharp
public void ShowChildName()
{
Console.WriteLine($"Child class Name: {Name}");
Console.WriteLine($"Base class Name: {base.Name}");
}
Solution 2: Using Type Casting
You can cast the derived class instance to the base class:
csharp
Copy code
BaseClass baseReference = (BaseClass)child;
Console.WriteLine(baseReference.Name); // Outputs: BaseClass
Updated Code Example
csharp
Copy code
public class Program
{
public static void Main()
{
ChildClass child = new ChildClass();
// Accessing variables
Console.WriteLine($"Child class Name: {child.Name}"); // Outputs: ChildClass
// Accessing base class variable using casting
BaseClass baseReference = (BaseClass)child;
Console.WriteLine($"Base class Name via casting: {baseReference.Name}"); // Outputs: BaseClass
}
}
Key Points
Variable hiding occurs when a derived class defines a variable with the same name as the one in its base class.
To access the hidden variable from the base class, you can use the base keyword (if inside a derived method) or cast the derived class instance to the base class type.
Method overriding is different from variable hiding. Method overriding uses the virtual and override keywords to provide a new implementation for methods in derived classes.
This way, you can control which version of the variable you want to access in your derived classes.
Comments
Post a Comment