Understanding C# Inheritance
Inheritance in C# is a fundamental concept of object-oriented programming that allows a class to inherit members (fields, methods, properties) from another class. The class that inherits is called the derived class, and the class being inherited from is called the base class.
Key Topics
Basic Inheritance
To inherit from a class, use the colon : followed by the base class name.
Example: Implementing Inheritance
public class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
dog.Eat(); // Inherited method
dog.Bark();
}
}
Output:
Eating...
Barking...
Explanation: The Dog class inherits from Animal, gaining access to its Eat() method.
Overriding Methods
You can override methods in the base class using the virtual and override keywords.
Example: Overriding a Method
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Meow");
}
}
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Animal();
Animal myCat = new Cat();
myAnimal.MakeSound(); // Outputs "Animal sound"
myCat.MakeSound(); // Outputs "Meow"
}
}
Output:
Animal sound
Meow
Explanation: The Cat class overrides the MakeSound() method of the base class Animal.
Using the base Keyword
The base keyword is used to access members of the base class from within a derived class.
Example: Calling Base Class Methods
public class Vehicle
{
public virtual void Start()
{
Console.WriteLine("Vehicle starting...");
}
}
public class Car : Vehicle
{
public override void Start()
{
base.Start(); // Calls the base class method
Console.WriteLine("Car started.");
}
}
class Program
{
static void Main(string[] args)
{
Car car = new Car();
car.Start();
}
}
Output:
Vehicle starting...
Car started.
Explanation: The Car class overrides the Start() method but also calls the base class implementation using base.Start().
Sealed Classes and Methods
The sealed keyword prevents a class from being inherited or a method from being overridden.
Example: Using Sealed Methods
public class A
{
public virtual void Display()
{
Console.WriteLine("Class A");
}
}
public class B : A
{
public sealed override void Display()
{
Console.WriteLine("Class B");
}
}
public class C : B
{
// Cannot override Display() because it is sealed in class B
}
class Program
{
static void Main(string[] args)
{
C obj = new C();
obj.Display(); // Outputs "Class B"
}
}
Output:
Class B
Explanation: The Display() method in class B is sealed, so it cannot be overridden in class C. When we call obj.Display(), it uses the implementation from class B.
Key Takeaways
- Inheritance allows a derived class to inherit members from a base class.
- Use the
virtualkeyword to allow a method to be overridden. - Use the
overridekeyword to override a base class method. - The
basekeyword accesses base class members from derived classes. - The
sealedkeyword prevents further inheritance or overriding.