What is Inheritance in Object Oriented Programming?
![What is Inheritance in Object Oriented Programming?](https://digitallatestnews.com/wp-content/uploads/2022/08/What-is-Inheritance-in-Programming-1-1.jpg)
When we talk about inheritance in programming we do not mean precisely that some distant relative has been able to leave us a fortune, we would like to. Actually it is one of the fundamental pillars of object-oriented programming. It is the mechanism by which a class allows inheriting the characteristics (attributes and methods) of another class.
Inheritance allows you to define new classes based on existing ones in order to reuse code, thus generating a hierarchy of classes within an application. If a class derives from another, it inherits its attributes and methods and can add new attributes, methods or redefine the inherited ones.
I am sure that when you read “reuse” your mouth is watering, right? There is nothing better in programming than being able to use the same code over and over again to make our development faster and more efficient. The concept of inheritance offers a lot of play. Thanks to this, we will achieve a much cleaner , structured code with fewer lines of code, which makes it more readable.
In Java we have to be clear about how to call the main class from which we inherit and the one that inherits from it, thus, the class that is inherited is called a superclass. The class that inherits is called a subclass. Therefore, a subclass is a specialized version of a superclass. It inherits all variables and methods defined by the superclass and adds its own unique elements.
Important terminology:
Superclass: The class whose characteristics are inherited is known as a superclass (either a base class or a main class).
Subclass: The class that inherits the other class is known as a subclass (or a derived class, extended class, or child class). The subclass can add its own fields and methods, in addition to the fields and methods of the superclass.
Reuse: Inheritance supports the concept of “reuse”, that is, when we want to create a new class and there is already a class that includes some of the code we want, we can derive our new class from the existing class. By doing this, we are reusing the fields/attributes and methods of the existing class.
Declare an inheritance hierarchy
In Java, each class can only be derived from another class. That class is called a superclass, or parent class. The derived class is called a subclass or child class.
It uses the keyword extends to identify the class that extends its subclass. If you don’t declare a superclass, your class implicitly extends the Object class. The object is the root of all inheritance hierarchies; It is the only class in Java that does not extend from another class.
Example of how to use inheritance in Java
To understand the concept better, we’ll create a superclass called Two Dimensions, which stores the width and height of a two-dimensional object, and a subclass called Triangle that we’ll use the extends keyword to create that subclass.
In the above program, when an object of class Triangle is created, a copy of all the methods and fields of the superclass acquires memory in this object. That is why, by using the subclass object, we can also access the members of a superclass.
Inheritance and access modifiers
Access modifiers define which classes can access an attribute or method. this could be used, for example, to be used to protect the information or rather define how our program accesses it. That is, access modifiers affect the entities and attributes that you can access within an inheritance hierarchy.
Although this may suddenly seem complex, the best way to understand it is to summarize its characteristics in a quick overview of the different modifiers:
- Private attributes or methods can only be accessed within the same class.
- Attributes and methods can be accessed without an access modifier within the same class, and by all other classes within the same package.
- Protected attributes or methods can be accessed within the same class, by all classes within the same package, and by all subclasses.
- All classes can access public attributes and methods.
As you can see from this list, a subclass can access all public and protected attributes and methods of the superclass. As long as the subclass and superclass belong to the same package, the subclass can also access all of the superclass’s package-private attributes and methods.
Of course, to see it more clearly, the best is always an example. There are many on the net, but I think one of the clearest and most complete is the example of the Coffee Machine program that you can find at the following link on GitHub . This code is also very interesting because other concepts appear such as overloaded method, polymorphism and abstract classes that we will see in future articles. So if you want, I invite you to download the code, analyze it completely and try to compile it to move forward.
First, the super keyword is used to call the constructor of the superclass. Since the constructor is public and can be accessed by the subclass, there is no problem creating the object.
Note: The super keyword can be used to access an attribute or to call a method of the superclass that the current subclass overrides.
But configMap is a protected attribute that can only be set using the Basic Coffee Machine class. By extending that class, the attribute also becomes part of the Premium Coffee Machine class, and I can add the configuration that is required to make an espresso to the Map.
Types of inheritance in Java
Java, like most modern programming languages, has different types of inheritance that we can use to make our program even more efficient by adding features or attributes from different classes. What we must take into account is that there can only be one superclass, as we will see in the following types of inheritance:
- Single Inheritance: Where subclasses inherit the characteristics of only one superclass.
- Multilevel Inheritance: A derived class will inherit a base class, and in addition, the derived class will also act as the base class of another class.
- Hierarchical Inheritance: A class serves as a superclass (base class) for more than one subclass.
- Multiple Inheritance (via interfaces): A class can have more than one superclass and inherit characteristics from all parent classes. But Java doesn’t support multiple inheritance with classes, so to achieve that we have to use Interfaces.
- Hybrid Inheritance (through Interfaces): It is a mixture of two or more previous types of inheritance. Since Java does not support multiple inheritance with classes, hybrid inheritance is also not possible with classes, but as in the previous example, we can achieve the same result through Interfaces.