What are the differences between Overrideing and overloading in Java?

What are the main differences between overrideing and overloading in Java? What relationship is there between these terms and polymorphism?

Author: LINQ, 2017-03-14

1 answers

Override (ing)

Is overwriting, i.e. defining a new behavior for a method that already exists. This happens when the class in question inherits (extends - extends) any other class and creates a method with the same signature as the "parent" class in the child class.

A simple example

public class Animal {
    public void mover() {
        System.out.println("Um animal pode se mover");
    }
}
public class Cachorro extends Animal {
    public void mover() {
        System.out.println("Cachorro pode caminhar e correr");
    }
}

Overload (ing)

Is the act of creating several different methods with the same name, but with different signatures, each with its own implementation. Specifically in Java, it is also widely used as a way to solve the "problem" of missing parameters default.

As for example (quite trivial, let's say in passing) a method enviarEmail with the implementation of the routine and another method only to fill the lack of parameters with values default.

public static void enviarEmail(String destinatario) {
    enviarEmail(destinatario, null);
}

public static void enviarEmail(String destinatario, Arquivos[] anexos) {
    MailMessage message = new MailMessage();
    
    if(anexos != null)
        message.addAttachments(anexos);

    message.Send();
}

In this way, the consumer of this service does not have to worry about passing parameters that are not used.

If the language had this feature, the method implementation could be something like

public static void enviarEmail(String destinatario, Arquivos[] anexos = null) {
    MailMessage message = new MailMessage();
    
    if(anexos == null)
        message.addAttachments(anexos);

    message.Send();
}

Okay, but what about polymorphism?

Starting from the principle, polymorphism is a word that comes from the junction of two Greek words, poly (many) and morph (forms). So, according to this definition it is possible to understand that polymorphism (in our context) is the ability of a call for a method to have a different behavior given a specific condition.

And how does it happen?

In the case of overriding of a method this is due to the fact that each specific type (those that extend a class) can have its own implementation of certain methods. Therefore, the same call of a method will end up calling the implementation within the class referring to its instance, thus making it possible to have different behaviors (various forms) in it call.

A simple example, based on the example at the beginning of this answer

public static void moverAnimal(Animal animal) {
    animal.mover();
}
Animal animal = new Animal();
moverAnimal(animal); // Neste caso, a saída vai ser > Um animal pode se mover

Animal cachorro = new Cachorro();
moverAnimal(cachorro); // Já aqui, a saída será > Cachorro pode caminhar e correr

Simple, right? The mover method can have several different behaviors (several forms).

In the case of overloading of methods is simpler still, the condition for this to happen is the existence (or absence) of a parameter.

As in the example above about overloading I focused on parameter simulation default , I will create another example (very simplistic too) here to be clearer.

public void trocarMusica() {
    Musica proxima = encontraProximaMusica();

    if(proxima != null)
        proxima.tocar();
}

public void trocarMusica(Musica proxima) {
    proxima.tocar();
}

Notice that the method that receives no parameter, only gets the next song from the list and, if there is a next one, starts playing it. The other method, on the other hand, receives a specific song by parameter and starts playing it.

Java's generics are also a form of polymorphism.

 22
Author: LINQ, 2020-06-11 14:45:34