Back

Inheritance

Idealogic’s Glossary

Inheritance is one of the four principles of Object Oriented Programming which enables one class acquires properties of another class. This feature allows the developers to define classes that are subordinate to other classes and this makes the code to be more reusable, efficient and organized within software systems. The class which is inherited is known as the derived class or subclass and the class from which the inheritance is going to be done is known as the base class or superclass.

How Inheritance Works

In the concept of inheritance, the sub class is able to acquire the features of the super class. This comes in handy in the sense that all the operations that can be performed in the parent class can also be performed in the child class without having to write the code again. Subclass can also declare additional attributes and methods which are not present in the superclass and can also redefine the inherited attributes and methods based on the requirements. This makes the subclass to implement the common properties of the parent class while at the same time having unique properties.

For instance, you have a Vehicle class with a movement method, the Car class is derived from Vehicle but it may alter the movement to represent how cars move in contrast to the other vehicles.

Types of Inheritance

In OOP there are several approaches to the polymorphism, which depends on the types of relationships between classes:

  1. Single Inheritance: A subclass is a class which is derived from another class and cannot be derived from more than one class. This is the most popular form of inheritance that is transfer from one generation to the next generation.
  2. Multiple Inheritance: Another property of the classes is that one of the classes that a subclass can be derived from can be more than one parent class.This type of inheritance cannot be implemented in all the programming languages as they may lead to complexities and misunderstanding.
  3. Multilevel Inheritance: A subclass is a class that is derived from another class known as the parent class which in the same way is derived from another class. It results into formation of a chain of inheritance.
  4. Hierarchical Inheritance: One or more sub-classes exist and they are sub-classes of the same super-class. This enable common features in different subclasses.
  5. Hybrid Inheritance: When two or more types of inheritance are used in a program this is known as hybrid inheritance.

Benefits of Inheritance

Inheritance provides several significant advantages in OOP: 

  1. Code Reusability: In the case of object-oriented programming, a subclass can inherit properties from a parent class thus avoiding having to write repeat the same code. This makes the development process faster and more efficient the reason being that the code does not have to be rewritten.
  2. Extensibility: Inheritance is a concept which allows a new class to be developed from an already developed class and new attributes can be added to the new class without altering the code of the original class. This entails that more functions can be created in the subclasses to enhance the class’s functionality and not necessarily change the base class.
  3. Maintainability: Modifications which are done to the parent class affect all the child classes as well. This helps in the maintenance and alteration of a code since changes are to be done once and not in several classes.
  4. Polymorphism Support: By means of polymorphism, several classes can be derived from a common parent class through the process of inheritance. In a way, this makes the software systems easier to understand since methods can accept objects of different classes while interacting with them through a single interface.

Drawbacks of Inheritance

While inheritance offers numerous advantages, there are also some potential drawbacks:

  • Increased Complexity: When the levels of inheritance are more, it becomes difficult to manage the code and it becomes complex in nature.
  • Tight Coupling: Subclasses are usually very dependent on their parent classes and this implies that any alteration made on the parent class may have undesirable effects on its subclasses. This could be a problem during the development phase and testing phases and also when changes are made on the code.
  • Overriding Risks: Subclass methods which have been defined to override some of the parent class methods may produce different results than the parent class methods.

Conclusion

Inheritance is one of the most important concepts that are used in the said paradigm of programming and it enables the construction of robust, adaptable and scalable applications. Through using inheritance, the code can be structured in a reasonable manner, the code can be made to be more concise, and the development process can be made much easier. But, it is crucial to appreciate that the use of inheritance can bring in complexity and the subclasses depend highly on the parent classes. If applied in the right way, inheritance is one of the most important principles which can improve the design and extensibility of the software systems in the OOP paradigm.