Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create new classes based on existing classes. In C#, inheritance enables the creation of derived classes (also known as subclasses or child classes) that inherit the properties and behavior of a base class (also known as a superclass or parent class). This relationship between classes is often referred to as an "is-a" relationship, indicating that a derived class is a specific type of the base class.
Syntax:
class { derived_class_name } : {base_class_name}
To illustrate inheritance, consider the following example:
class Shape
{
protected int width;
protected int height;
public void SetDimensions(int w, int h)
{
width = w;
height = h;
}
}
class Rectangle : Shape
{
public int GetArea()
{
return width * height;
}
}
class Program
{
static void Main()
{
Rectangle rect = new Rectangle();
rect.SetDimensions(5, 3);
int area = rect.GetArea();
}
}
In this example, we have a base class `Shape` and a derived class `Rectangle`. The `Rectangle` class inherits from the `Shape` class. This means that all Public, Internal and Protected members of the `Shape` class are accessible in the `Rectangle` class. Private members of the base class are not accessible in the derived class.
Inheritance allows code reuse, as the derived class inherits the attributes and behaviors of the base class. It facilitates the creation of a hierarchical structure of classes, where each derived class specializes or adds functionality to the base class. This inheritance hierarchy can be extended further by creating additional derived classes from existing derived classes.
Types of Inheritance:
There are several types of inheritance that you can use to establish different relationships between the base and derived classes. Let's explore the most commonly used types of inheritance:
1. Single Inheritance:
Single inheritance is the most basic and commonly used type of inheritance. In single inheritance, a derived class inherits from a single base class.
Example:
class Base
{
// Base class members
}
class Derived : Base
{
// Derived class members
}
2. Multilevel Inheritance:
Multilevel inheritance involves a chain of inheritance, where a derived class becomes the base class for another derived class. It allows the creation of a hierarchy of classes with each derived class inheriting properties and behaviors from its immediate base class.
Example:
class Base
{
// Base class members
}
class Derived1 : Base
{
// Derived class 1 members
}
class Derived2 : Derived1
{
// Derived class 2 members
}
3. Hierarchical Inheritance:
Hierarchical inheritance involves multiple derived classes inheriting from a single base class. In this type of inheritance, different derived classes extend the functionality of the base class in different ways, creating a hierarchy of classes.
Example:
class Base
{
// Base class members
}
class Derived1 : Base
{
// Derived class 1 members
}
class Derived2 : Base
{
// Derived class 2 members
}
Constructor Call Sequence in Inheritance:
1. Base Class Constructors:
2. Derived Class Constructor:
using System;
class Base
{
private int baseData;
public Base(int data)
{
this.baseData = data;
Console.WriteLine("Base Constructor with data: " + baseData);
}
}
class Derived : Base
{
private int derivedData;
public Derived(int data1, int data2) : base(data1)
{
this.derivedData = data2;
Console.WriteLine("Derived Constructor with data: " + derivedData);
}
}
class Program
{
public static void Main()
{
Derived derivedObj = new Derived(10, 20);
}
}