Key Difference – Multiple vs Multilevel Inheritance
Object-Oriented Programming (OOP) is a paradigm to design a program using classes and methods. Real world scenarios can be mapped to objects. Therefore, it is easy to build software solutions. A class is a blueprint to build an object. It contains properties and methods. For example, before creating a student object, there should be a class student with properties and methods. A student can have properties such as student id, name and methods such as read, write, study. Methods describe the behaviour while properties are the attributes. After creating the class, it is possible to create objects using them. Object creation is also known as Object Instantiation. Object does not exist in isolation. They communicate with other objects and data is passed within objects. One pillar of OOP is Inheritance. The purpose of Inheritance is to increase code reusability. It creates a new class of properties and methods of the already existing class. The existing class is known as the base class, and the new class is called the derived class. Multiple Inheritance and Multilevel Inheritance are inheritance types. This article discusses the difference between them. The key difference between Multiple and Multilevel Inheritance is that Multiple Inheritance is when a class inherits from many base classes while Multilevel Inheritance is when a class inherits from a derived class making that derived class a base class for a new class.
CONTENTS
1. Overview and Key Difference
2. What is Multiple Inheritance
3. What is Multilevel Inheritance
4. Similarities Between Multiple and Multilevel Inheritance
5. Side by Side Comparison – Multiple vs Multilevel Inheritance in Tabular Form
6. Summary
What is Multiple Inheritance?
Multiple Inheritance is when a class inherits more than one base class.
A B and C are classes. A and B are base classes, and C is the derived class. The class C has to manage the dependency of both base classes A and B. Multiple inheritances is not widely used in software projects. It makes the system more complex because one class is inheriting many classes.
For example, assume that class A and B both have a method with the same name which is the sum() and class C is deriving both classes. After creating an object of type C and calling sum () method, it can cause an error because both classes have the same method. The compiler does not know which function to call. Therefore, Multiple Inheritance increases the complexity of a system. Multiple Inheritance is supported in C++ language but languages such as Java, C# do not support Multiple Inheritance. Instead, these languages use interface that is similar to a class but cannot be instantiated.
What is Multilevel Inheritance?
Multilevel Inheritance is when a class inherits from a derived class making that derived class a base class for a new class.
Multilevel inheritance has three levels. The intermediate class that B inherits from class A and class C inherits from class B. A is the base class for B and B is the base class for C.
A program that implements Multilevel Inheritance is as follows. The program is written using Java.
According to the above program, class A is the base class for class B. Class B is the base class for class C. All properties and methods of class A is accessible by class B. All properties and methods of class B is accessible by class C. Therefore, class C can access properties and methods of both A and B. When creating an object of type C, it is possible to call all three method A (), B () and C (). The output will give A, B, C.
What is the Similarity Between Multiple and Multilevel Inheritance?
- Both are types of Inheritance.
What is the Difference Between Multiple and Multilevel Inheritance?
Multiple Inheritance vs Multilevel Inheritance |
|
Multiple Inheritance is an Inheritance type where a class inherits from more than one base class. | Multilevel Inheritance is an Inheritance type that inherits from a derived class, making that derived class a base class for a new class. |
Usage | |
Multiple Inheritance is not widely used because it makes the system more complex. | Multilevel Inheritance is widely used. |
Class Levels | |
Multiple Inheritance has two class levels namely, base class and derived class. | Multilevel Inheritance has three class levels namely, base class, intermediate class and derived class. |
Summary – Multiple vs Multilevel Inheritance
Inheritance is a major pillar of Object Oriented Programming. There are different types of Inheritance; they are Single Level Inheritance, Multilevel Inheritance, Multiple Inheritance, Hierarchical Inheritance and Hybrid Inheritance. The Single Level Inheritance has one base class and one derived class. Hierarchical Inheritance has one base class and many derived classes. The Hybrid Inheritance is a combination of Multilevel and Multiple Inheritance. This article described the difference between Multiple Inheritance and Multilevel Inheritance. The difference between Multiple and Multilevel inheritances is that Multiple Inheritance is when a class inherits from many base classes while Multilevel Inheritance is when a class inherits from a derived class, making that derived class a base class for a new class. Multilevel Inheritance is widely used than Multiple Inheritance.
Download the PDF Multiple vs Multilevel Inheritance
You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here Difference Between Multiple and Multilevel Inheritance
Reference:
1.Singh, Chaitanya, et al. “Types of inheritance in Java: Single,Multiple,Multilevel & Hybrid.” Beginnersbook.com, 6 Dec. 2013. Available here
2.tutorialspoint.com. “Java Inheritance.” The Point. Available here
Image Courtesy:
1.’Multiple Inheritance’By Suman Maverick Gangulian Suman420 – Own work, (CC BY 3.0) via Commons Wikimedia
2.’Multilevel Inheritance’By Author: Suman Maverick Gangulian Suman420 (talk) 12:03, 2 March 2015 (UTC) – Own work, (CC BY 3.0) via Commons Wikimedia
Leave a Reply