Inheritance is one of the core principles of Object-Oriented Programming (OOP). It allows a class (subclass or child class) to acquire the properties and behaviors of another class (superclass or parent class). This promotes code reuse, scalability, and maintainability.
Inheritance is a mechanism where a child class derives properties and behaviors from a parent class. The child class can:
- Use the fields and methods of the parent class
- Override parent class methods to provide a specific implementation
- Add its own additional properties and methods
- Code Reusability: Avoids code duplication by reusing fields and methods of the parent class.
- Improves Maintainability: Reduces redundancy, making code easier to manage.
- Enhances Extensibility: New functionality can be added easily without modifying existing code.
The parent class contains common fields and methods.
using System;
// Parent class
class Animal {
public string Name;
public void Eat() {
Console.WriteLine(Name + " is eating...");
}
}
The child class inherits the properties and methods of the parent class.
// Child class
class Dog : Animal {
public void Bark() {
Console.WriteLine(Name + " is barking...");
}
}
Now, let's create an object and use the inherited methods.
class Program {
static void Main() {
Dog myDog = new Dog();
myDog.Name = "Buddy";
myDog.Eat(); // Inherited from Animal class
myDog.Bark(); // Defined in Dog class
}
}
Buddy is eating...
Buddy is barking...
C# supports different types of inheritance:
A subclass inherits from one superclass.
class Parent {
public void Show() {
Console.WriteLine("This is the parent class");
}
}
class Child : Parent {
public void Display() {
Console.WriteLine("This is the child class");
}
}
A subclass inherits from another subclass, forming a chain.
class Grandparent {
public void Show() {
Console.WriteLine("Grandparent class");
}
}
class Parent : Grandparent {
public void Display() {
Console.WriteLine("Parent class");
}
}
class Child : Parent {
public void Print() {
Console.WriteLine("Child class");
}
}
A single parent class has multiple child classes.
class Parent {
public void Show() {
Console.WriteLine("Parent class");
}
}
class Child1 : Parent {
public void Display() {
Console.WriteLine("Child1 class");
}
}
class Child2 : Parent {
public void Print() {
Console.WriteLine("Child2 class");
}
}
Note: C# does not support multiple inheritance (i.e., a child class inheriting from multiple parents) due to ambiguity problems.
Method overriding allows a child class to redefine a method from the parent class.
class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Dog barks");
}
}
class Program {
static void Main() {
Animal myAnimal = new Dog(); // Polymorphism
myAnimal.MakeSound();
}
}
Dog barks
The base
keyword is used to refer to the parent class. It helps to:
- Call the parent class constructor.
- Access the parent class methods.
- Access the parent class fields.
class Animal {
public Animal() {
Console.WriteLine("Animal Constructor");
}
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal {
public Dog() {
Console.WriteLine("Dog Constructor");
}
public override void MakeSound() {
base.MakeSound(); // Calls parent method
Console.WriteLine("Dog barks");
}
}
class Program {
static void Main() {
Dog myDog = new Dog();
myDog.MakeSound();
}
}
Animal Constructor
Dog Constructor
Animal makes a sound
Dog barks
class Employee {
public string Name;
public double Salary;
public Employee(string name, double salary) {
Name = name;
Salary = salary;
}
public virtual void DisplayDetails() {
Console.WriteLine("Employee: " + Name + ", Salary: " + Salary);
}
}
class Manager : Employee {
public double Bonus;
public Manager(string name, double salary, double bonus) : base(name, salary) {
Bonus = bonus;
}
public override void DisplayDetails() {
base.DisplayDetails();
Console.WriteLine("Bonus: " + Bonus);
}
}
class Program {
static void Main() {
Manager manager = new Manager("Alice", 70000, 10000);
manager.DisplayDetails();
}
}
Employee: Alice, Salary: 70000.0
Bonus: 10000.0