I'm studying Java and I need to understand the logic of a code here. I wanted to know what this snippet does:
public class UsuarioController extends HttpServlet {
private DAO dao;
public UsuarioController() {
super();
}...
I'm studying Java and I need to understand the logic of a code here. I wanted to know what this snippet does:
public class UsuarioController extends HttpServlet {
private DAO dao;
public UsuarioController() {
super();
}...
The super()
is for calling the superclass constructor. It is always called, even when it is not explicit in the code, when it is explicitly it should be the first item inside the constructor.
See for example the code below:
public class Teste {
public static void main(String[] args) {
new Sub();
}
}
class Super {
public Super() {
System.out.println("super");
}
}
class Sub extends Super {
public Sub() {
System.out.println("sub");
}
}
Output:
super
sub
Although the Test class is instantiating an object of the Sub class, both the Sub and Super constructors are called.
In Java super()
invokes the constructor, without arguments, of the derived class (parent).
In your example, and since UsuarioController
extends class HttpServlet
will invoke the default constructor of class HttpServlet
.
The super
directive, without parentheses, also allows you to invoke methods of the class that was derived using the following syntax.
super.metodo();
This is useful in cases where you override (override) a method of the parent class and wish to invoke the original method.
The subclass can overwrite methods of the superclass and, of course, implement their own methods. Each class has two references: this , which references the instance itself and the super reference to superclass .
In practice it works like this:
public class Pessoa {
private String nome;
public Pessoa() {
// Construtor padrão.
}
public Pessoa(String nome) {
this.nome = nome; // Aqui fazemos referência a instância da classe
}
public void chorar() {
System.out.print("Pessoa chorando");
}
public void greet() {
System.out.print("Olá "+this.nome);
}
}
public class Chaves extends Pessoa {
public void chorar() {
System.out.print("pi pi pi pi pi pi pi ");
super.chorar(); // Aqui eu invoco o método da superclasse se eu quiser.
}
}
Note that the person class has a greet()
method, which is publicly accessible in any instance of Pessoa
and its subclasses, only nome
is only accessible by the Pessoa
constructor, now what? Simple: Just change the constructor of Chaves
, like this:
public Chaves() {
super("Chaves"); // Lembra que temos um construtor com argumentos em Pessoa?
}
I can call other superclass methods too:
public foo() {
this.fazAlgumaCoisa();
super.fazOutraCoisa();
/* também é possível usar this.fazOutraCoisa(). Se eu não sobrescrever o
método fazOutraCoisa eu prefiro usar o super, deixando claro que este
método está na superclasse. Faço assim para facilitar aleitura*/
}
Some interesting things we should know about super
are as follows:
When declaring a class without the default constructor (no arguments) jvm creates one for you as follows:
public Classe() {
super(); // Aqui o super chama o construtor da superclasse;
}
When we talk about constructors, super()
must always be the first method to be called:
public Classe() {
this.fazAlgumaCoisa();
super(); // erro de compilação
}
public Classe() {
super(); // Ok
this.fazAlgumaCoisa(); //OK
}
The use of super is not obligatory and it is good practice to make correct use of it to make reading the code easier. Here are some recommendations:
We should call the constructor of superclasse
only if it makes sense. Doing so makes it clear to other programmers: Hey, there's something important going on in the superclass. Be smart!
Methods that are inherited and not overridden should never be invoked using this.metodoHerdado()
, prefer super.metodoHerdado()
. This will make it clear to other programmers that this method is implemented in the superclass and not in the class it is working on. This is just a recommendation.
Use to call the constructor of the parent class of the class that is calling super();
It can also be called with parameters if the parent class has a constructor with the appropriate parameter, such as super(parametro);
In your case, you will call the HttpServlet()
constructor of the parent class.
To study well for the test, read the java manual: link