INHERITANCE IN JAVA
INTRODUCTION
Inheritance in Java is the basis of OOP. It is a pillar of OOP programming that all object-oriented languages implement in some way. It is based on generalization, which is a relationship between classes and allows a set of concepts to be organized in a hierarchy, from which classes are arranged from the most particular to the most general. Inheritance actually constitutes this relationship between classes. Let us see with a picture how we can implement generalization in Java.
It starts with the keyword class since we are in fact creating a class, followed by the name of this subclass and the keyword extends
indicating that we are deriving from the superclass, followed by the name of the superclass itself. Java supports single inheritance, that is, we can derive from a single superclass unlike other languages such as C++.
I will use two classes to talk about inheritance: AClass and BClass, the latter a specialization of the former.
INHERITANCE OF MEMBERS OF A CLASS
There are two instance attributes in the superclass, while no members are defined in the derived class. A first thing we can do with inheritance is to use the members of the superclass in the subclass since the members of the superclass are inherited. Let’s look at the test class to clarify the concept.
As you see the instance attributes of the superclass are automatically inherited in the subclass. Now, however, we need to make a point. We know that as a rule we should never make the instance attributes of a class public, this is for security reasons, let’s make them private.
We get an error from the compiler as private attributes cannot be accessed even from a derived class.
Actually this is what we wanted, we create a public method in the superclass to display the instance attributes.
Let’s see what happens if we invoke the method from the subclass. As you can see, since the instance attributes of the superclass have not been initialized we get their default values, false for booleans and null for strings. The thing to understand is that in addition to the attributes we also inherit the public methods of the superclass.
CONSTRUCTORS AND INHERITANCE
Each class in Java can define its own constructors, in the presence of inheritance first the constructor of the superclass is executed then that of the subclass.
Let’s see what happens with the test class.
What happens is that when creating the instance of BClass the empty and parameterless constructor of AClass, and then the attributes are initialized. However, if we wanted to decide the value of attributes from the subclass all this would be impossible with the code written so far. Let’s modify the code.
Let us see how the subclass should be modified.
We can in the parameterless constructor of BClass invoke the constructor of the parent class using the keyword super and passing in parentheses the arguments that initialize the attributes of the superclass. super must appear as the first instruction, this is because we said that first the superclass instance must be built, it will be the compiler based on the arguments passed to choose the right superclass constructor to call, or to generate an error if there is no match. Let’s look at the test class.
LINKS TO PREVIOUS POSTS
LINK TO CODE ON GITHUB
EXECUTION OF THE EXAMPLE CODE
- Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.
java -jar –enable-preview CorsoJava.jar
- Or run the main found in the file CorsoJava.java.
Leave A Comment