Understanding Inheritance
Understanding Inheritance in C#
In the world of object-oriented programming, inheritance allows us to define a class based on another class. This facilitates code reusability and makes your program more modular. In this article, we will delve into the concept of inheritance in C#, and explore its many facets.
What is Inheritance?
Inheritance is a pillar of Object-Oriented Programming (OOP). It allows you to create a new class (child class or derived class) from an existing class (parent class or base class). The derived class inherits all the features from the base class and can also have its own unique features.
Here is a simple example:
public class Animal {
public void eat() {
Console.WriteLine("Eating...");
}
}
public class Dog : Animal {
public void bark() {
Console.WriteLine("Barking...");
}
}
In the above example, Dog
is the derived class and Animal
is the base class. The Dog
class inherits the eat()
method from the Animal
class.
Types of Inheritance
C# supports several types of inheritance:
- Single Inheritance: A class is allowed to inherit from only one class.
- Multilevel Inheritance: A class can inherit from a derived class, making that derived class a base for the new class.
- Hierarchical Inheritance: A single class serves as a superclass (base class) for more than one subclass.
// Single Inheritance
public class BaseClass {
public void method() {
Console.WriteLine("BaseClass method");
}
}
public class DerivedClass : BaseClass {
// inherits method from BaseClass
}
// Multilevel Inheritance
public class SecondDerivedClass : DerivedClass {
// Inherits method from BaseClass through DerivedClass
}
// Hierarchical Inheritance
public class DerivedClass2 : BaseClass {
// Inherits method from BaseClass
}
The base
Keyword
In C#, the base
keyword is used to access members of the base class from within the derived class:
public class BaseClass {
public void Show() {
Console.WriteLine("BaseClass Show Method");
}
}
public class DerivedClass : BaseClass {
public new void Show() {
base.Show(); // Calls Show method of BaseClass
Console.WriteLine("DerivedClass Show Method");
}
}
In the above example, base.Show()
is used to call the Show()
method of the BaseClass
.
The sealed
Keyword
The sealed
keyword in C# is used to prevent inheritance of a class or prevent overriding of methods.
If a class is declared as sealed
, it cannot be inherited:
sealed class SealedClass {
public void Show() {
Console.WriteLine("Sealed Class");
}
}
In the above example, SealedClass
cannot be used as a base class.
Conclusion
Inheritance is a fundamental concept in C# and other object-oriented programming languages. It allows for code reusability, making your applications more modular, easier to read, write and maintain.
Remember, a derived class extends the base class, meaning it inherits all members of the base class. We can also prevent a class from being inherited by marking it with the sealed
keyword. Remember that understanding and using inheritance effectively can help you to design more efficient and effective C# programs.