What are the differences between overrideing and overloading in Java?

Asked

Viewed 5,228 times

14

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

1 answer

22


Override(ing)

is overwritten, ie set a new behavior for a method that already exists. This happens when the class in question inherits (extends - extends) any other class and a method is created with the same signature of the class "father" in the class daughter.

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.

For example (quite trivial, in passing) a method enviarEmail with the implementation of the routine and another method only to supply 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();
}

Thus, the consumer of this service need not 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 beginning, polymorphism is a word that comes from the joining of two Greek words, Poly (many) and Morph (forms). So, according to this definition it is possible to understand that polymorphism (in this context of ours) is the ability of a call to a method to have a different behavior given a specific condition.

And how does that happen?

In the case of Overriding of a method this is due to the fact that each specific type (those extending 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 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, isn’t it? The method mover may have several different behaviors (various forms).

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

As in the example above about overloading I focused on the simulation of parameters default, I’ll 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();
}

Note that the method that does not receive any parameter only gets the next song from the list and, if there is a next one, starts playing it. The other method, receives a specific song per parameter and starts playing it.

The Generics of Java are also a form of polymorphism.

  • I read it all, you can be carefree! Very good. Vlw

  • Cool example is much clearer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.