Inheritance in OOPs

Inheritance in OOPs

August 25, 2022
4 min read
6695 views
0

Inheritance

Object-oriented programming creates reusable patterns of code to curtail redundancy in development projects. OOPs achieves recyclable code through inheritance. And Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object and create another class. A class that is used as the basis for inheritance is called a superclass or base class and the class that inherits from a superclass is called a subclass or derived class. Inheritance allows us to create classes that are built upon existing classes, to specify a new implementation to maintain the same behavior, to reuse code and to independently extend original software via public classes and interfaces.

Parent Classes - Parent or base classes create a pattern in which child or subclasses can be based on. Parent classes allow you to create child classes through inheritance without having to write the same code over again each time. You can make any class into a parent class, so they are each fully functional classes in their own right rather than just templates.

Child Classes - Child or subclasses are classes that will inherit from the parent class, means that each child class will use the methods and variables of the parent class.

Modes of Inheritance

Public mode - If you derive a subclass from a public base class then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class. Private members of the base class will never get inherited in the subclass.

Protected mode - If you derive a subclass from a protected base class then both public member and protected members of the base class will become protected in the derived class. Private members of the base class will never get inherited in the subclass.

Private mode - If you derive a subclass from a private base class then both public member and protected members of the base class will become Private in the derived class. Private members of the base class will never get inherited in the subclass.

Types of Inheritance

OOPs supports the six types of inheritance. There are mentioned below -

Single Inheritance

When a Derived Class to inherit properties and behavior from a single Base Class, it is called as single inheritance.

Single Inheritance

Multi-level inheritance

A derived class can also inherit by another class, that means when a derived class again will be inherited by another class then it creates a Multiple Levels.

Multi level inheritance

Multipath inheritance

In this inheritance, a derived class is created from another derived class and the same base class of other derived classes.

Multipath Inheritance

Multiple inheritance

Multiple Inheritances is that in which a Class inherits the features from two Base Classes When a Derived Class takes Features from two Base Classes.

Multiple Inheritance

Hybrid inheritance

Hybrid Inheritance is implemented by combining more than one type of inheritance.

Hybrid Inheritance

Hierarchical inheritance

More than one derived class are created from a single base class, is called Hierarchical Inheritance.

Hierarchical

Advantages

  • Reduces code redundancy and source code size.

  • Provides code re-usability and improves code readability.

  • The code is more manageable and divided into parent and child classes.

  • Supports code extensibility by overriding the base class functionality within child classes.

Disadvantages

  • Base class and child classes are tightly combined in Inheritance. So, If you change the parent class code, it will affect to the all the child classes.

  • In the class hierarchy, many data members remain unused and the memory allocated to them is unutilized, and it affects the performance of your program if you have implemented inheritance incorrectly.

Visual gallery and highlight images for Inheritance in OOPs
Inheritance in OOPs - Image 1 of 4
Inheritance in OOPs - Image 2 of 4
Inheritance in OOPs - Image 3 of 4
Inheritance in OOPs - Image 4 of 4
1 / 4
Was this article helpful?Vote to let the author know
0
Share this article

Loading comments...

Related Articles

Why Did Apple Really Remove the Headphone Jack? The $15 Billion Truth Nobody Talks About

Why Did Apple Really Remove the Headphone Jack? The $15 Billion Truth Nobody Talks About

Apple removed the 3.5mm headphone jack from the iPhone 7, claiming it took "Courage." They told us it was to make phones thinner, improve waterproofing, and deliver better audio. But 10 years later, looking at the numbers, was it really about design—or was it a $100 Billion illusion?

8 views
Read More
Read full article: Why Did Apple Really Remove the Headphone Jack? The $15 Billion Truth Nobody Talks About
Cloud Hosting India: Which Provider is the Best? Complete Guide

Cloud Hosting India: Which Provider is the Best? Complete Guide

Cloud hosting uses a network of interconnected servers rather than relying on a single physical machine. Your website or application runs on virtualized infrastructure, allowing resources such as CPU, RAM, storage, and networking to be allocated according to workload requirements

11 views
Read More
Read full article: Cloud Hosting India: Which Provider is the Best? Complete Guide
How to Connect a Headless CMS to Your React App

How to Connect a Headless CMS to Your React App

React remains the most-used JavaScript framework, with the State of JS 2025 survey putting adoption at 83.6%. A growing share of that base pairs React with a react js cms rather than hardcoding content.

123 views
Read More
Read full article: How to Connect a Headless CMS to Your React App