Inheritance and Types of Inheritance
Definition
Inheritance is the mechanism in object-oriented programming by which one class, called the derived class or child class, acquires the properties and methods of another class, called the base class or parent class.
In simple words, inheritance allows a new class to reuse, extend, and modify the behavior of an existing class. The base class provides common attributes and functions, while the derived class can add new features or override existing ones to suit specific needs.
Main Content
1. Inheritance
- Inheritance is a relationship in which one class gets the characteristics of another class, forming a parent-child structure.
- It promotes code reusability, readability, and easier maintenance because common code is written only once in the base class.
Inheritance helps in modeling real-world relationships efficiently. For example, if Vehicle is a general class, then Car, Bike, and Truck can inherit from it. Each child class automatically gets general features like speed, fuel type, or motion methods, while also adding its own unique features. This avoids repeating the same code in every class.
Inheritance also supports the principle of extensibility. A programmer can create a new class based on an existing one and then modify or improve it without disturbing the original code. This is especially useful in large software systems, where many classes may share common behavior.
2. Base Class and Derived Class
- The base class, also called the parent class or superclass, contains common attributes and methods that are shared.
- The derived class, also called the child class or subclass, inherits those features and can also define additional features of its own.
The base class acts as a general template. It defines the basic properties that are common to a group of related classes. For example, a class Person may contain common data such as name, age, and gender, along with methods like displayDetails().
The derived class represents a more specific version of the base class. For example, Student can inherit from Person and add attributes like rollNo, course, and marks. This way, the Student class does not need to redefine all the common person-related data.
A derived class may also override methods of the base class when it needs specialized behavior. For instance, a Teacher class and a Student class may both inherit from Person, but their displayDetails() method may show different information.
3. Types of Inheritance
- Inheritance can be classified into several types based on how classes are related.
- The major types are single inheritance, multilevel inheritance, hierarchical inheritance, multiple inheritance, and hybrid inheritance.
Single Inheritance:
In single inheritance, one derived class inherits from one base class only. This is the simplest form of inheritance.
Example: Dog inherits from Animal. The Dog class gets common properties from Animal and can add its own methods such as bark().
Multilevel Inheritance:
In multilevel inheritance, a class is derived from another derived class, creating a chain of inheritance.
Example: Animal -> Mammal -> Dog. Here, Dog inherits features from Mammal, and Mammal inherits from Animal. As a result, Dog gets properties from both levels above it.
Hierarchical Inheritance:
In hierarchical inheritance, multiple derived classes inherit from the same base class.
Example: Animal as a base class, and Dog, Cat, and Bird as derived classes. All these classes share common features from Animal, but each one has its own specific behavior.
Multiple Inheritance:
In multiple inheritance, one derived class inherits from more than one base class.
Example: SmartPhone may inherit from both Phone and Camera. This allows the new class to combine features from both parent classes. However, multiple inheritance can create ambiguity if both base classes contain methods with the same name.
Hybrid Inheritance:
Hybrid inheritance is a combination of two or more types of inheritance. It is used in complex class relationships.
Example: A structure may include both hierarchical and multiple inheritance together. Hybrid inheritance is more flexible, but it may be more complex to implement and manage.
Working / Process
- The programmer first identifies the common features that should be placed in a base class.
- A derived class is then created using inheritance so it can reuse those features and add new ones.
- The child class can access inherited members, override methods if needed, and create specialized behavior for its own purpose.
When a derived class object is created, the base class portion is also initialized first. This ensures that shared data and functions are ready before the child-specific features are used. The child class can then call inherited methods directly or redefine them for different output.
For example, if Vehicle is the base class and Car is the derived class, the process begins by storing common attributes like speed and fuel. Then Car adds features like brand, numberOfDoors, and a method such as openTrunk(). If Car needs a different version of start(), it can override that method.
In many programming languages, inheritance is implemented using specific syntax such as extends or a colon (:), depending on the language. The general process remains the same: define a parent class, derive a child class, and then use or modify inherited behavior.
Advantages / Applications
- It promotes code reusability and reduces duplication of repeated code.
- It improves maintainability because changes made in the base class automatically reflect in derived classes.
- It supports real-world modeling, making software design more natural, organized, and easy to understand.
Inheritance is widely used in practical software development. It is helpful in designing systems such as banking applications, employee management systems, educational software, vehicle simulation programs, and graphical user interface frameworks. For example, in a banking system, a common Account class can be inherited by SavingsAccount and CurrentAccount.
It also plays a major role in implementing polymorphism, where different derived classes respond differently to the same method call. This makes programs flexible and powerful. Inheritance therefore not only saves time but also improves scalability and structured design.
Summary
- Inheritance allows one class to acquire and reuse features of another class.
- It creates a parent-child relationship that supports code reuse and specialization.
- Common types include single, multilevel, hierarchical, multiple, and hybrid inheritance.
- Inheritance is important for building organized, reusable, and scalable programs.