Inheritance in C#

Mannan Ul Haq
0

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:

When you have a derived class that inherits from a base class, the constructor call sequence determines the order in which the constructors are executed during the creation of an object. The constructor call sequence ensures that the base class is initialized before the derived class.

Here's a simplified explanation of the constructor call sequence:

1. Base Class Constructors:

The constructors of the base classes are called first. If there are multiple base classes, they are initialized in the order they are listed in the derived class's inheritance declaration. Each base class is responsible for initializing its own data members.

2. Derived Class Constructor:

After the base class constructors are executed, the constructor of the derived class is called. The derived class constructor initializes the data members specific to the derived class.

By following this sequence, the derived class can rely on the fully constructed base class when initializing its own members.

Let's consider an example to explain the constructor call sequence in inheritance:

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);
    }
}

Output:

Base Constructor with data: 10
Derived Constructor with data: 20

When an object of the derived class `Derived` is created with the statement `Derived derivedObj = new Derived(10, 20);`, the constructor call sequence is as follows:

1. The parameterized constructor of the base class `Base` is called with the argument `10`. It outputs "Base Constructor with data: 10".

2. After the base class is initialized, the parameterized constructor of the derived class `Derived` is called with the arguments `20`. It outputs "Derived Constructor with data: 20".

By passing the appropriate arguments to the constructors, you can initialize the data members of both the base and derived classes.


Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !