We live in a world where we find software and programming everywhere. The modern-day programming languages are strongly built on the Object-Oriented Programming (shortly abbreviated as OOPs) concept. OOP is a paradigm that helps programmers structure their code so that they are easy to maintain, understand, and override. The four pillars of the OOP paradigm are Abstraction, Encapsulation, Inheritance and Polymorphism. Now, instead of me bragging more about OOPs, let us start with Inheritance. Because that's what this tutorial is all about.
What is Inheritance?
The term 'Inheritance' is derived from the word "Inherit" which means, "to derive any quality, features or characteristics from family". Therefore, "Inheritance is defined as a mechanism where the sub or child class inherits the properties and characteristics of the super class or other derived classes. It also supports additional features of extracting properties from the child class and using it into other derived classes."
In computer programming, Inheritance is one of the most important concepts followed by Abstraction, Encapsulation and Polymorphism in the Object Oriented Programming (OOPS) Paradigm. This mechanism allows the new objects to take on the properties of existing objects. Inheritance builds relationships within the classes; therefore, relating two or more classes to each other and establishing class hierarchies. Rather than writing the same code, again and again, the programmers can use the code that is already created by inheriting its class.
To dig deep more into inheritance, firstly we need to understand the two types of programming classes
An inherited class is known as Sub Class or Derived Class. Sub Class inherits the properties or characteristics from Base Class, also popularly known as Super class.
In the programming world, Sub Class is also referred to as a child class, whereas the superclass is referred to as parent class.
How Does Inheritance Make Programming Easy?
In object-oriented programming, a programmer creates a class and defines objects, data members and data functions in that class. Data members are a reference to objects (of any type), or the variables declared in a class where Data functions are defined as methods or functions designated in the class. Coders use these functions to manipulate the data members. These data members and member functions can be declared using the keyword public or private, depending upon the scope of use.
With the OOPs Inheritance mechanism, programmers need not recursively create the data members and data functions in the same code. They create and define the data member and function once in the superclass, and they make the derived class inherit all attributes and properties of data members and functions of its superclass. Therefore supports effective dynamic programming making work easier.
Types of Inheritance
1. Single Inheritance:
In this type, the child class inherits the properties from the superclass.
In the below example, you will notice class One is the superclass, and class Two is the base class. Therefore, class Two inherits the properties and behaviour of the base class One.
2. Multiple Inheritance:
The child class inherits the properties and features from two or more parent classes with this type.
In the below example, you will notice class Three inherits the features and behaviour of class Two. Further, class Two inherits the properties of class One. Therefore, we can conclude that class One is the base class of class Two, whereas class Two is the base class of class Three.
Hence, class Three implicitly inherits the behaviour and properties of class One along with class Two, thereby creating a multiple inheritance.
3. Multilevel Inheritance:
With this type, one child class inherits the properties and behavior from two or more superclasses.
In the below example, class three inherits the properties, functions and objects for both class Two and class One at the same level. Therefore, both class One and class Two are the superclasses for class Three.
4. Hierarchal Inheritance:
In the Hierarchical Inheritance type, two or more child classes inherit the properties and behaviors from one superclass.
In the below example, the following hierarchies:
(I) The base class One has two derived classes, i.e., class Two and class Three.
(II) Next, class Two acts as a base class for two child classes named Six and Seven.
(III) Further, class three is also a base class for class Four and class Five, respectively.
5. Hybrid Inheritance:
With this type, the programmer can call a combination of two or more types of inheritance. Therefore the code can include a combination of Multiple and Hierarchical inheritance, Multilevel and Hierarchical inheritance or Hierarchical and Multipath inheritance, or it may be a combination of three of them, i.e., Multiple, Multilevel and Multiple Hierarchical inheritance.
NOTE: The .NET Languages such as C#, F# etc., does not support hybrid inheritance.
Advantages of Inheritance
Given below are some advantages of Inheritance.
What is the importance of Inheritance in Programming?
Inheritance is important in programming because it prevents code duplicity and data redundancy. Therefore, the prime reason for using Inheritance is because it supports code reusability. Let's understand the above statement using a real-world example: Suppose you and your brothers live with your father, then the father will be one parent or superclass, and you and your brothers will be the derived or child classes. Therefore, the child classes (you and your brothers) inherit many features from your parent class (father) such as last name, house address line, house landline number, city, country, etc., And recently you all moved to a new place, and now the address needs to be changed. So instead of rewriting the new address in all the classes, we update the address of the superclass, and all the derived classes will inherit the new address from that superclass.
What is the Need for Inheritance?
Let's understand how the Inheritance concept works with the top four OOPs oriented programming languages.
Inheritance in C++
In C++, Inheritance is a process that allows the new objects to take on the attributes and properties of existing parent objects. Therefore, it increases code reusability and allows the programmers to alter the properties of data members and member functions defined in other classes.
Refer to the below syntax to inherit a superclass into the child class in C++.
Inheritance in Java
In Java, Inheritance is a mechanism in which one object inherits all the attributes and behaviors of a data members and member functions available in the parent class. Refer to the below syntax to inherit a superclass into the child class in Java
NOTE: Java does not support multiple Inheritance.
Inheritance in Python
In Python, a child class can inherit the attributes and behaviors of a superclass by just declaring the name of the superclass in the bracket after the child class's name. Refer to the below syntax to inherit a superclass into the child class in Python.
Inheritance in C#
In C# inheritance is a process of building relationships within the classes; therefore, relating two or more classes to each other and establishing class hierarchies automatically. Refer to the below syntax to inherit a superclass into the child class in C#.
Next TopicFriend Definition