Inheritance

inheritance, inheritance in oops, inheritance in c++, inheritance in java, inheritance in C#

What is inheritance?

Inheritance is the process of deriving quality from the parent. In object-oriented programming (OOP), it is the process of creating a class from another class called a base class. So that derived class(child class) can acquire the property, methods of a base class.

Derive (a quality, characteristic, or predisposition) genetically from one’s parents or ancestors.

We can say that inheritance would be a process to derive a quality, characteristic, or predisposition from ancestors.

In simple terms, we can state that Inheritance in object-oriented programming (OOP) means a class is defined in a way so that it takes on the properties and methods of a different class by establishing a parent-child relationship between those classes.

Inheritance is the process in which one class acquires(inherits) the properties of other classes.

It helps to resolve a real-time issue and promote reusability in programming. So, think of it is as the property of a person which by law is available to the children of that person.

For example, consider a class animal; all animal has a name, color, and have types like vertebrates.

Suppose a class called ‘Tiger’ a tiger has a name, color, we can define them into types along with this it can have other attributes too.

Another class called ‘Cat’ could have the same properties and attributes along with other attributes.

While designing software, it would make sense if the ‘Tiger’ class and the ‘Cat’ class could reuse the attributes and behaviors of the ‘Animal’ class.

It implements an “IS-A relationship”. For example, ‘Tiger’ IS A animal, ‘Cat’ IS-A animal.

Why we need inheritance?

Without inheritance to we would be simply copying and paste data members and methods in the class in which we want them to be present. This means mean we need to be duplicating the code. Moreover, after duplicating this code, we find any error in one of the methods, we will have to rectify that error in the original place as well as all the places where we have duplicated the code. It helps to resolve all this issue also it allows derived class to access all the members of the parent class without the code duplication.

C# Syntax of inheriting one class from another (Note: as per programming language below syntax may vary):

namespace Demo
{
    class BaseClass
    {
        //-members of parent
    }


    class DerivedClass: BaseClass
    {
        //We can make use of the members of the parent class as well as we can add more feature to DerivedClass
    }

}

In the above example, Derived (Child) class can make use of the members of the Base (Parent) class except for the private members of the Base (Parent) class.

Read more about Access Specifiers

Let’s take an example of real time example “Shape” class.

using System;

namespace Demo
{
    class Shape
    {
        public int Width { get; set; }
        public int Height { get; set; }

    }

    class Rectangle : Shape
    {
        public int CalculateArea()
        {
            return (Width * Height);
        }
    }
    class Program
    {
        static void Main()
        {
            Rectangle rect = new Rectangle {Width = 5, Height = 7};

            Console.WriteLine("Total area: {0}", rect.CalculateArea());
            Console.ReadLine();
        }
    }
}

In the above example, we have a “Shape” class and it contains two variables height and width. “Rectangle” class has derived from Shape class. In the Main() method, we are simply creating an instance of the “Rectangle” class, and then we can see that all the members of the “Shape” class.

Points to remember

  1. A child class can access all the members of the Parent class, except for the private members in the same project.
  2. Child class also inherits the constructor of the parent class.
  3. A child class can access the members of the Parent class; however, the parent class cannot access any members of the Child class.

Types of Inheritance

There are 5 different types of inheritance in the Object-Oriented Programming (OOP).

  • Single
  • Multi-Level
  • Hierarchical
  • Multiple
  • Hybrid

Single

In Single Inheritance, a derived (child) class has one Base (parent) class only and a Base (parent) class has one derived (child) class only.

Single inheritance ,Single inheritance in Java, Single inheritance in C++, Single inheritance in C#,

Multi-Level

When there is a chain sequence of inheritance it’s called multi-level inheritance. For example, ClassC is derived from ClassB, ClassB is derived from ClassA. So, the parent class has one child class and that child class is a parent to another class and so on the chain of an endowment.

Multi-Level inheritance Multi-Level inheritance in Java, Multi-Level inheritance in C++, Multi-Level inheritance in C#,  types of inheritance in java, types of inheritance in C++, types of inheritance in C#

Hierarchical

When a Base (Parent) class has more than two children then it’s known as Hierarchical Inheritance.

Hierarchical inheritance in Java, Hierarchical  inheritance in C++, Hierarchical  inheritance in C#,  types of inheritance in java, types of inheritance in C++, types of inheritance in C#

Multiple

Multiple Inheritance is when one derived (child) class can have multiple base (parent) classes. Most of OOP language does not support this type.

Multiple inheritance in Java, Multiple inheritance in C++, Multiple inheritance in C#,  types of inheritance in java, types of inheritance in C++, types of inheritance in C#

Hybrid

Hybrid inheritance is a combination of multi-level, hierarchical, and multiple inheritances.

Hybrid inheritance in Java, Hybrid inheritance in C++, Hybrid inheritance in C#,  types of inheritance in java, types of inheritance in C++, types of inheritance in C#

In any object-oriented programming language, It represents the same concept i.e. process of deriving quality from the parent. So, inheritance in c++, inheritance in java, inheritance in C# conceptual represent the same. Read more oops about polymorphism in oops, encapsulation in oop, abstraction in oop

OOPs Inheritance Resource


Leave a Reply

Your email address will not be published. Required fields are marked *