INHERITANCE IN JAVA

SUPER AND MEMBERS OF SUPERCLASSES

java-logo


super
, in addition to the use we have seen in constructors, can also be used to access members of a superclass from a subclass. In these examples for demonstration purposes only we will use non-private attributes in classes. Remember attributes must always have the modifier private so that they are only accessible within the class in which they are declared. Let’s look at a new version of AClass.

Definizione AClass

THE ACLASS AND BCLASS

We have made the attributes non-private again; you will see why in a moment. Let’s look at the change to the subclass.

Definizione BClass

We have redefined the attribute st, same type and same name as the one in the superclass. In fact redefining that attribute, BClass
hides the top-level variable. Let’s look at the class of test.

Classe di test

When we create the instance of BClass superclass and subclass constructor are invoked, respectively. The method viewAttr() which we invoke via the instance bc, correctly displays the attribute st of the superclass, when we get to the println the attribute st invoked is that of the subclass.

Classe di Test

You may be wondering, what does keyword super have to do with this? Let’s see what happens by using super to access the st attribute of the superclass from the subclass. The call to super is not only limited in the constructor, but we can use it in any other method.

keyword super

Let’s look at the test class.

chiamata tramite super alla superclasse

HIERARCHY OF INHERITANCE

In the examples we have seen, our generalization strategy stopped at only one level. See the figure below.

gerarchia di classi

We have defined an inheritance hierarchy, CClass is the subclass of BClass while the same BClass is now both subclass and superclass.

gerarchia di ereditarietà

I remind you that in the constructor of BClass we must invoke with the first statement the constructor of the superclass AClass by passing it the argument of type String a because that is what the constructor expects. We introduce CClass and analyze the code.

definizione di CClass

CClass has a constructor that takes three parameters; it is called super with two parameters a and b that are used to initialize the top-level attributes a for AClass, b for BClass. The c parameter is used to initialize the CClass instance attribute. We have access to the attributes of AClass and BClass because let us remember they are not private. Let’s look at the test class.

Test CClass

VARIABLES, REFERENCE, INHERITANCE

We define two methods, one in the superclass and one in the subclass.

Metodi di AClass e BClass

Let’s look at the test class.

main

If we now try to make the assignment b=a that is, we assign a b the superclass reference we get a compiler error.

Errore del compilatore

If we do the opposite a=b this time we have no problem.

Assegnamento di reference

We try with the superclass instance variable to call the BClass method. We still get an error.

Errore del compilatore

This means that even if the superclass has the same reference as the subclass it cannot access it as it can only access its own members. Let us summarize what we have seen so far and derive a general rule from it. We have two variables a and b of type AClass and BClass that point to objects of the same classes. At first we have this situation (see figure).

Situazione dei reference Iniziale

Then we placed b = a and we saw that it is not possible to assign the variable b to an object of the superclass. (see figure)

Assegnamento di reference

Then we swapped the references and saw that it is possible to assign a variable of the superclass to an object of its subclass. However, even doing this the variable a of the superclass continues to see the AClass object.

Assegnamento di reference

This brings us to this final conclusion. It is only the type of the variable i.e. the reference type that determines which members can be accessed through this variable, while the object type is irrelevant. This means that once the variable a has been defined to be of type AClass even if we assign it an object created as an instance of BClass, this object is seen however as an object of type AClass so we can access only the members defined in the superclass.

Conclusioni
Conclusioni

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

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.