Welcome to The Coding College! In this tutorial, we’ll dive into C# inheritance, one of the key concepts of Object-Oriented Programming (OOP). Inheritance allows a class to reuse, extend, and override the functionality of another class, making your code modular and reusable.
What is Inheritance in C#?
Inheritance enables you to define a class (child class or derived class) that derives from another class (parent class or base class). The derived class inherits fields, properties, methods, and other members of the base class, allowing you to reuse and extend functionality.
Key Terminologies:
- Base Class: The class being inherited from.
- Derived Class: The class that inherits from the base class.
Syntax of Inheritance
class BaseClass
{
// Base class members
}
class DerivedClass : BaseClass
{
// Derived class members
}
Example of Inheritance
Code:
public class Animal
{
public string Name;
public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine($"{Name} is barking.");
}
}
public class Program
{
public static void Main()
{
Dog dog = new Dog();
dog.Name = "Buddy";
dog.Eat(); // Inherited method
dog.Bark(); // Method in the derived class
}
}
Output:
Buddy is eating.
Buddy is barking.
Types of Inheritance
C# supports the following types of inheritance:
- Single Inheritance: A class inherits from one base class.
- Multilevel Inheritance: A class inherits from a derived class, forming a chain.
- Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
Single Inheritance Example
public class Vehicle
{
public void Start()
{
Console.WriteLine("Vehicle started.");
}
}
public class Car : Vehicle
{
public void Drive()
{
Console.WriteLine("Car is driving.");
}
}
public class Program
{
public static void Main()
{
Car myCar = new Car();
myCar.Start(); // Inherited method
myCar.Drive(); // Method in derived class
}
}
Multilevel Inheritance Example
public class LivingBeing
{
public void Breathe()
{
Console.WriteLine("Breathing...");
}
}
public class Mammal : LivingBeing
{
public void Walk()
{
Console.WriteLine("Walking...");
}
}
public class Human : Mammal
{
public void Think()
{
Console.WriteLine("Thinking...");
}
}
public class Program
{
public static void Main()
{
Human human = new Human();
human.Breathe(); // Inherited from LivingBeing
human.Walk(); // Inherited from Mammal
human.Think(); // Method in Human
}
}
Using the base
Keyword
The base
keyword is used to access members of the base class in a derived class.
Example:
public class Parent
{
public void Display()
{
Console.WriteLine("This is the Parent class.");
}
}
public class Child : Parent
{
public void Show()
{
base.Display(); // Accessing the base class method
Console.WriteLine("This is the Child class.");
}
}
public class Program
{
public static void Main()
{
Child child = new Child();
child.Show();
}
}
Output:
This is the Parent class.
This is the Child class.
Method Overriding
Derived classes can override base class methods using the virtual
and override
keywords.
Example of Method Overriding:
public class Animal
{
public virtual void Sound()
{
Console.WriteLine("Animal makes a sound.");
}
}
public class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Dog barks.");
}
}
public class Program
{
public static void Main()
{
Animal animal = new Dog();
animal.Sound(); // Calls the overridden method
}
}
Output:
Dog barks.
Access Modifiers and Inheritance
The accessibility of base class members determines their visibility in derived classes:
public
: Accessible in derived classes.protected
: Accessible in derived classes, but not outside the class hierarchy.private
: Not accessible in derived classes.internal
: Accessible within the same assembly.protected internal
: Accessible within the same assembly and derived classes.
Benefits of Inheritance
- Code Reusability: Avoid duplication by reusing base class functionality.
- Extensibility: Add new features to existing classes without altering their code.
- Modularity: Keep related functionality grouped in classes for better maintenance.
Best Practices for Inheritance
- Use inheritance when there is a clear “is-a” relationship between classes.
- Keep base classes simple to avoid unintended consequences in derived classes.
- Prefer composition over inheritance when functionality doesn’t strongly depend on the parent class.
- Avoid deep inheritance hierarchies, as they make debugging and understanding the code difficult.
Conclusion
Inheritance is a cornerstone of Object-Oriented Programming, providing the tools to build modular, maintainable, and reusable code. By leveraging inheritance wisely, you can simplify your projects and enhance the overall structure of your applications.
Explore more C# tutorials and OOP concepts at The Coding College and take your coding skills to the next level. Happy learning! 🚀