java-logoINHERITANCE IN JAVA

METHOD OVERRIDES

In this post we will discuss one of the most important topics underlying inheritance in Java. In previous lessons we introduced in concept of overload, let’s look at a figure.

Method overload

We know that we can define methods having the same name as long as the number and type of parameters is different (different signature Overload). We will now see with inheritance what happens if we declare in the subclass a method having the same signature (Override). See the figure.

Override dei metodi

What we have done is called method overrides.

Method Override

A method override occurs when in a subclass we go to redefine a method present in the superclass, provided that this method has the same name, same return type, and same signature (i.e., the same number and type of parameters). We create the test class.

Ridefinizione del metodo nella sottoclasse

As you see the method redefined in BClass is executed. We need to do this when in a subclass we want to redefine in an integral way the behavior of a method that is in the superclass. Now we don’t always want that but most of the time we want to extend the behavior of a method in the superclass, using override and extending its functionality. Then we call back with the keyword super the superclass method to have the standard behavior, then we specialize the subclass. The figure clarifies the concepts exposed, first we invoke the standard behavior of the method by invoking it with super on the superclass, then we specialize it in the subclass.

la keyword super

Let’s pay attention to one thing.

Overload

In this case we did not get the override of the methods rather the overload since in the subclass in myMethod the number of parameters is different. We can use either version indifferently, depending on our needs.

OVERRIDE AND POLYMORPHISM

In this section we go to see why override is so important in OOP. A very important feature of the override is the Dynamic Method Dispatch, put simply this means that Java is able to override a method by choosing it at runtime and not at compile time, and this leads as a consequence that another form of polymorphism can be implemented. Let’s look at an example.

We have three override methods each with different behavior.

Polimorfismo

We are going to create an Array of AClass objects. We now have an arr variable that is of type AClass, but actually contains an Array that points to lower-level objects. See the figure.

Polimorfismo

In the foreach loop, dynamic type selection occurs at runtime; in the presence of overrides, objects referenced by a variable are chosen, not those in the defining class. The figure summarizes what happens. This is the most important form of polymorphism presented in Java and if well managed allows for very sophisticated programs.

Polimorfismo

ABSTRACT METHODS AND ABSTRACT CLASSES

Let’s take the previous example again. See figure.

Polimorfismo

We may want to define more general behavior in the superclass, in particular we may want to define a method that itself has no behavior, it is like a kind of placeholder that forces the person extending the class to implement it. To do this we use the keyword abstract.

Metodo abstract

A class can also mix the two, that is, it can have some methods that are abstract and others that are not. However, there is a rule to follow, if a class contains even one abstract method the whole class becomes abstract.

Classi astratte

An abstract class cannot be instantiated. If a class does not intend to redefine the abstract methods of the superclass then it too must be declared abstract.

Classi astratte

B2Class can be instantiated and redefine the method.

THE KEYWORD FINAL

final is used to prevent method overrides, specialization of a class, and also to create constants. Let us take an example we have already seen.

keyword final

In some situations we want the definition of a method to remain as such by preventing its redefinition and override in derived classes.

keyword final

We can also define the class final and prevent its inheritance.

keyword final


final
finally can be used to transform any variable into a constant.

final double PIGRECO = 3.14169

THE OBJECT CLASS

Object is the base class for all classes in Java, both the language’s default classes and those we create. So at the top of any inheritance chain is the Object class. When we declare a class without keyword extends, without knowing it we are deriving from Object. It contains a whole series of methods, some final so they must be used as is, while others can be redefined. We will place attention on two of these methods: equals and toString.

We could also write AClass in this way because as we said at the top of the inheritance chain is the Object class.

La superclasse Object

The equals method is an instance method of the Object class and has an Object type (any class) as a parameter.

boolean equals(Object obj);

Metodo equals

It returns false because the two objects are clearly different; in fact, they do not have the same reference. However, if we assign a=b this time the result is true because the two objects have the same reference. They point to the same memory area. Let’s look at toString().

The method returns a viewable version of an object. Returns the qualified name of the Data Type of the AClass instance followed by the @ symbol and a hexadecimal number representing the HashCode() of the object, which is a unique number that identifies the object itself.

metodo toString()

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.