The main difference between abstraction and inheritance is that abstraction allows hiding the internal details and displaying only the functionality to the users, while inheritance allows using properties and methods of an already existing class.
Object-Oriented Programming (OOP) is a major programming paradigm. It allows the developers to model real-world scenarios using a set of objects easily. Many modern programming languages such as Java and C# support OOP. Class and object are the building blocks of an OOP program. A class is a blueprint, whereas an object is an instance of a class. Additionally, there are pillars of OOP. Two of them are abstraction and inheritance.
Key Areas Covered
Abstraction, Abstract Class, Inheritance, Interface, Object, OOP
What is Abstraction
Abstraction is the methodology of hiding the internal details and displaying only the functionality to the users. In other words, the user or the external world does not see the internal implementation details. Instead, he only sees the functionality. Abstraction concept is similar to the following example. For example, assume a remote controller. It consists of a set of buttons to operate. The user can change the channels, increase the volume, change the brightness etc. He does not have to know about the internal circuitry of the controller to operate. Abstraction is similar to that.
Programming languages such as Java support Abstraction. The programmer can implement abstraction using concepts such as abstract class and interface. Firstly, an abstract class can consist of abstract and non-abstract methods. A class that extends an abstract class have to provide the implementations for the abstract methods. Secondly, an interface is a collection of abstract methods. It does not have non-abstract methods. Therefore, the class that implements an interface has to provide the implementations or method definitions to all abstract methods in the interface. Overall, abstraction helps to reduce the complexity of the system.
What is Inheritance
Inheritance is the methodology of using properties and methods of an already existing class in a new class. The existing class is the parent or superclass while the new class is the child or subclass. The developer does not have to write the code from the beginning. Instead, he can use the already existing code and develop the rest. Therefore, inheritance allows code reusability.
There are various types of inheritance. They are as follows.
Single level Inheritance – If A and B are two classes and B inherits A, then it is a single level inheritance.
Multi-level Inheritance – There is an intermediate class. There are three classes called A, B and C. B inherit from A and C inherits from B.
Multiple Inheritance – If A, B and C are three classes, and if C inherits from both A and B, then it is multiple inheritance.
Hierarchical Inheritance – When A, B and C are three classes, and B and C inherits from A, then it is a hierarchical inheritance.
Hybrid Inheritance – It is a combination of multilevel and multiple inheritances.
Difference Between Abstraction and Inheritance
Abstraction is an OOP concept that hides the implementation details and shows only the functionality to the user. In contrast, Inheritance is the methodology of creating a new class using the properties and methods of an existing class. Thus, this reflects the main difference between abstraction and inheritance.
Moreover, another difference between abstraction and inheritance is that abstraction helps to reduce the complexity of the code, while Inheritance helps to improve code reusability.
Overall, abstraction and inheritance are entirely different OOP concepts, and they provide different uses. The main difference between abstraction and inheritance is that abstraction allows hiding the internal details and displaying only the functionality to the users, while inheritance allows using properties and methods of an already existing class.
1.”inheritance diagram” By Pluke – Own work (CC0) via Commons Wikimedia.