What are the differences between overrideing and overloading in Java?

14

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

    
asked by anonymous 14.03.2017 / 12:58

1 answer

21

Override (ing)

is overwriting, that is, setting a new behavior for a method that already exists. This happens when the class in question inherits (extends - ) any other class and creates a method with the same parent class signature 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 default parameters.

As for example (very trivial, by the way) a enviarEmail method with the implementation of the routine and another method only to supply the lack of parameters with default values. >

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 implementation method 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?

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

And how does this happen?

In the case of overriding a method this is due to the fact that each specific type (those that extend a class) may have its own implementation of certain methods. So, the same call of a method will end up calling the implementation within the class for its instance, thus making it possible to have different behaviors (various forms) on the same 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 (many forms).

In 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'll create another example (very simplistic too) here to get clearer.

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

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

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

Notice that the method that does not receive any parameter, only gets the next song from the list and, if there is a next song, it starts to play it. Already the other method, it receives a specific song by parameter and begins to touch it.

Generics of Java are also a form of polymorphism.

    
14.03.2017 / 14:01