What is the purpose of @Override?

39

I have some doubts about @override , I read somewhere and I vaguely remember the rewriting issue, but what is it? And what's the use? As it applies to a JAVA code. Is there any other language?

    
asked by anonymous 30.06.2014 / 16:37

4 answers

43

It is a way to ensure that you are overwriting a method and not creating a new one.

Let's say you created a class with a print method:

public class SuperClasse {
    public void imprime() {
        System.out.println("imprime");
    }
}
So you've made a class that extends this class, and you want to change what will be printed when the method is called:

public class MinhaClasse extends SuperClasse {
    public void imprime() {
        System.out.println("imprime diferente");
    }
}

You have correctly overridden the imprime() method and the above code will work without any major issues.

One fine day, however, you decided to rename the method in SuperClass from imprime() to imprimir() :

public class SuperClasse {
    public void imprimir() {
        System.out.println("imprime");
    }
}

If you do not remember the class that extended the SuperClasse you will have a imprime() method in it that is not overwriting the method in SuperClasse . You are actually calling a new method, which is called imprime() .

What would happen if you had used the annotation @Override?

This would be your code at first for both classes:

public class SuperClasse {
    public void imprime() {
        System.out.println("imprime");
    }
}

public class MinhaClasse extends SuperClasse {
    @Override
    public void imprime() {
        System.out.println("imprime diferente");
    }
}

So far, nothing new. However when you change your method from imprime() to imprimir() you will no longer be able to compile your code because @Override will notice that you are not overwriting anything since there is no more imprime() method in SuperClasse. >

You will receive the following error:

  

MyClass should override or implement a supertype method

In (free) translation:

  

MyClass should overwrite or implement a method of its superclass

Every object-oriented language allows superclass methods to be overridden by the child class. However, each programming language uses its own means to deal with this overwriting. Java has chosen to use the annotation @Override for developers who want the security mentioned in the course of the response, however, nothing obliges the use of this annotation.

C # also has such functionality, however it does not use annotation, it incorporates the reserved word override in the method declaration:

public override int Area()
{
    return side * side;
}

It is impossible to list all programming languages that use override because believe it or not, there are thousands (literally!) of languages out there. If you want a slightly more complete list see: Wikipedia - Method overriding

    
30.06.2014 / 17:15
12

What is Override in Object Oriented Programming?

Rewrite a method that was inherited, where its behavior in the parent class differs from its behavior in the child class. That is, they have the same name but different functionalities or actions. If you do not rewrite the method it will have the behavior of the parent class, which by logic is only an implementation .

Does it exist in other languages?

In Object Oriented languages, this is a trivial factor, there are perhaps things that change in the way it is implemented.

Example:

public class Pai {    
    public int Soma(int value){
        return value + 100;
    }
}

public class Filho extends Pai {
    @Override
    public int Soma(int value){
        return value + 200;
    }
}

Coding

public static void main(String[] args) 
{
    Pai pai = new Pai();
    System.out.println(pai.Soma(1));

    Filho filho = new Filho();        
    System.out.println(filho.Soma(1));
}

Result

Note that in the parent class the value is one and in the class Daughter is another, ie how it was rewritten, you may have different behaviors in the classes

>     
30.06.2014 / 17:06
10

Overwriting or Override

Overriding a method occurs when a child class implements a method that already exists in a parent class by overwriting existing behavior.

Example:

public class Gato {
    public void falar() {
        System.out.println("Miau");
    }
}

public class Gatinho extends Gato {
    public void falar() {
        System.out.println("Mew");
    }
}

In the example above, class Gatinho inherits class Gato , but changes the behavior of falar() method.

If we call the methods polymorphically, the result is different. Example:

Gato gato = new Gato();
gato.falar();
Gatinho gatinho = new Gatinho();
gatinho.falar();
Gato gatoPolimorfico = new Gatinho();
gatoPolimorfico.falar();

The result will be:

  

Meow

     

Mew

     

Mew

Class java.lang.Object

Specifically in the Java language, there is the Object class that is the parent of all other classes. This means that, even though it does not have a extends in the declaration, every class has an implicit inheritance.

The Object class has some special methods. One of them is toString() , used to "represent" an object in the form of String . In this case, we could overwrite the method even without having an explicit inheritance.

Example:

public class Gato {
    public String toString() {
        return "Sou um Gato";
    }
}

Subscriptions of overwritten methods

There is a detail of method overriding that was not mentioned and often causes problems: To override a method, you must keep the same signature .

What is a signature in the Java language? It is the composition of the elements that declare the method.

Consider the following signature:

public String setNome(String nome) throws IllegalArgumentException

Now consider the elements that are part of a method declaration:

  • Access modifiers : public , private , protected
  • Other modifiers : static , native , synchronized
  • Return Type : String , int
  • Method name : setNome
  • Parameter types and names : String nome
  • Declared exceptions : IllegalArgumentException

Specifically in Java, method overriding occurs if at least the name and type of the parameters are the same.

The following example describes a method overload ) and not an override:

public class OutroGatinho extends Gato {
    public void falar(String frase) {
        System.out.println(frase);
    }
}

In the above class, considering the first example, the method falar(String) is is overwriting the falar() method of class Gato , because the signature is different.

Another feature of the Java language is that it is case-sensitive ( case sensitive ). This can lead to misconceptions, as the following example:

public class Gato {
    public String tostring() {
        return "Sou um Gato";
    }
}

Note that s is lowercase in tostring . This causes the above method not to be called as expected!

Annotation @Override to redemption!

Then came the annotation @Override . It simply informs the compiler that intent would overwrite.

In this way, the compiler can evaluate whether the signature is consistent with some superclass method and issues a warning if the method is not actually overwriting something!

So, in the example of tostring lowercase, it would be clear to the developer that there was some misunderstanding.

In addition, it is common when a system's code is modified or libraries are updated, methods that the overwritten developer will simply fail to be called without warning. This is because a method of an overwritten class has been changed or removed. With the @Override annotation, you'll be safer to make that kind of change and be able to track the code points that need attention.

    
30.06.2014 / 17:35
1

Overwriting methods is part of the object orientation that I will not go into detail, since the question is annotated.

The annotation basically serves to "warn" the compiler that that method is overriding a superclass method from which it extends.

    
30.06.2014 / 16:45