I think the Java documentation explains it quite clearly, the problem is that it is in English.
Let me leave it here translated, so that it helps the understanding of future programmers, since it is an interesting question, especially when we start programming in Java.
Use of the keyword super
Access to Superclass members
If your method replaces one of the methods of your superclass, you can invoke the overwritten method (override) by using the keyword super
. You can also use super
to refer to a hidden field (although hiding fields is discouraged).
Consider this class, Superclase
:
public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
Here is a subclass, called Subclase
, which replaces (overwrites)%% of%:
Here is a subclass, called Subclass, that overrides printMethod ():
public class Subclass extends Superclass {
// sobreescribe printMethod en Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
}
Within the Subclass, the simple name printMethod ()
refers to the one declared in Subclass, which replaces the one of Superclass. Therefore, to refer to printMethod()
inherited from printMethod()
, Superclase
must use a qualified name, using Subclase
as shown. When we compile and execute super
print the following:
Printed in Superclass.
Printed in Subclass.
Subclass builders
The following example illustrates how to use the keyword Subclase
to invoke the constructor of a superclass. Remember the example super
that Bicycle
is a subclass of MountainBike
. Here is the constructor Bicycle
(subclass) that calls the constructor of the superclass and then adds an initialization code of its own:
public MountainBike(int startHeight,
int startCadence,
int startSpeed,
int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
The invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
super();
o:
super (lista de parámetros);
With MountainBike
, the constructor is called without arguments from the superclass. With super()
, the superclass constructor is called with a corresponding parameter list.
Note: If a constructor does not explicitly invoke a constructor of
superclass, the Java compiler automatically inserts a call to the
constructor without arguments of the superclass. If the superclass does not have
a constructor without an argument, you will get an error in time of
compilation. The object has such a constructor, so if the object is
the only superclass, no problem.
If a subclass constructor invokes a constructor of its superclass, explicitly or implicitly, you can imagine that there will be a complete string of called constructors, all the way back to the Object constructor. In fact, this is the case. It is called constructor chain , and it is necessary to be aware of this when there is a long line of descent of class.