Understanding the Base Keyword in C# with a Practical Example
Understanding the Base
Keyword in C# with a Practical Example
When working with inheritance in
C#, the base keyword is an essential tool that helps you reuse
and extend functionality from a parent class. It allows a derived class to call
constructors and methods defined in its base class, ensuring proper
initialization and consistent behavior.
In this blog, we will explore
the base keyword through a simple but illustrative example
involving employee management.
What is the Base Keyword?
The base keyword
refers to the immediate parent class of a derived class. It lets you:
- Call the base class constructor
from the derived class.
- Access or invoke overridden
methods from the base class inside the derived class.
This helps avoid duplication and
ensures that the base class is properly initialized and its logic is preserved.
Real-World Scenario: Employees and
Managers
Let’s consider a company where we
have different types of employees. We will create a base class Employee with
common properties like name and salary, and a derived class Manager that adds
specific details such as the size of the manager’s team.
The Code
using System;
public class Employee
{
public
string Name { get; }
public
decimal Salary { get; }
// Base
class constructor
public
Employee(string name, decimal salary)
{
Name
= name;
Salary
= salary;
Console.WriteLine("Employee
constructor called");
}
// Virtual
method that can be overridden
public
virtual void DisplayInfo()
{
Console.WriteLine($"Name:
{Name}");
Console.WriteLine($"Salary:
{Salary:C}");
}
}
public class Manager : Employee
{
public int
TeamSize { get; }
// Derived
class constructor calls base constructor
public
Manager(string name, decimal salary, int teamSize) : base(name, salary)
{
TeamSize
= teamSize;
Console.WriteLine("Manager
constructor called");
}
//
Override method to extend base functionality
public
override void DisplayInfo()
{
base.DisplayInfo(); //
Call base class method
Console.WriteLine($"Team
Size: {TeamSize}");
}
}
class Program
{
static
void Main()
{
Manager
manager = new Manager("Alice Johnson", 90000m, 10);
manager.DisplayInfo();
}
}
How This Works
Calling the Base Constructor
The Manager constructor needs to
initialize the properties inherited from the Employee class (Name and Salary).
It does this by calling the base class constructor explicitly using base(name,
salary). This ensures the Employee part of the object is properly set up before
the Manager class adds its own property TeamSize.
Overriding and Extending Methods
The Employee class has a virtual
method DisplayInfo() that prints basic employee details. The Manager class
overrides this method to add team size information.
Instead of replacing the entire
method, it first calls the base implementation with base.DisplayInfo() to reuse
the existing logic, then adds the manager-specific details. This makes the code
cleaner and avoids duplication.
Output Explanation
When you run the program, the
output will be:
Employee constructor called
Manager constructor called
Name: Alice Johnson
Salary: $90,000.00
Team Size: 10
This shows the order of
constructor calls and the combined output from both classes, illustrating
proper inheritance and reuse.
Why Use the Base Keyword?
- Proper Initialization: Ensures
base class constructors are called and base properties are set.
- Code Reuse: Enables
derived classes to build upon existing base class methods without
rewriting code.
- Cleaner Design: Keeps
your class hierarchies organized and maintainable.
Conclusion
The base keyword
is a powerful feature in C# for working with inheritance. It lets you leverage
and extend functionality from parent classes in a clean and efficient way.
By understanding and using
the base keyword, you can write more robust, reusable, and
maintainable object-oriented code.
Comments
Post a Comment