Inheritance is a word that consists of the word “Inherit”, which means “To Derive”. So, Inheritance is defined as one class’s tendency to derive properties and characteristics from other classes. It provides additional functionalities to extract features from the base class and imply them into other derived classes significantly.
In computer programming, Inheritance is an eminent concept in Object Oriented provides a Programming (OOPS) Paradigm. It provides a mechanism for establishing relationships and building hierarchies of class in object composition. Inheritance means the use of code that is pre-written or created previously. And one thing to keep in mind is that we are just using the code and not updating or changing it. The functions and methods defined in one class may be used in manipulating other data members of the class.
What are the different types of Inheritance?
Here are some types of Inheritance:
- Single inheritance: One derived class inherits from one base class.
- Multiple Inheritance: One derived class inherits from many base classes.
- Multilevel Inheritance: one derived class inherits from other derived classes.
- Hierarchal Inheritance: More than one derived classes inherit from one base class.
- Hybrid Inheritance: A combination of more than one type of inheritance.
What are some advantages of Inheritance?
Here are some advantages listed of Inheritance:
- Frequent use of code written once, which means code reusability.
- One superclass can be used for the number of subclasses in a hierarchy.
- No changes to be done in all base classes; just do changes in parent class only.
- Inheritance is used to generate more dominant objects.
- Inheritance avoids duplicity and data redundancy.
- Inheritance is used to avoid space complexity and time complexity.
Why is Inheritance Important in Programming and why should we use it?
Inheritance is important in programming due to code reusability. We can avoid duplicate data and redundancy from our program. For example, if you live with your parents, then the father will be one base class, and you (as a child) will be the derived class. So, we can inherit many things such as last name, address line, city, and state from the parent class. We use inheritance to use specific portions of code and modify certain features according to our needs, and this can be done without any complexity. Inheritance provides flexibility in our code to reuse it from base class to required class.
Why do we need Inheritance?
- To reuse code, write code and apply it further, wherever necessary.
- To avoid duplicity and data redundancy in the program.
- To reduce space and time complexity.
- Easier in hierarchal programming paradigm.
- Variables of the same name can be used multiple times in the scope of the code.
- To create dominant data objects and functions.
I chose this topic because I already knew about Inheritance before due to my previous computer science classes and used it in my Java programming projects. I was just curious to dig in more and learn about its potential and importance in programming.
What are the Different Types of Inheritance? (with pictures) (wise-geek.com)
What is Inheritance in Programming | Object Oriented Concept (educba.com)
From the blog CS@Worcester – Software Intellect by rkitenge91 and used with permission of the author. All other rights reserved by the author.