Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Meaning of @override in Android Studio [duplicate]

I am completely new to Android Studio and I want to know the purpose of the @Override statement in Android Studio.

like image 587
user359187 Avatar asked Jan 19 '11 14:01

user359187


People also ask

What is @override used for?

The @Override annotation is one of a default Java annotation and it can be introduced in Java 1.5 Version. The @Override annotation indicates that the child class method is over-writing its base class method. It extracts a warning from the compiler if the annotated method doesn't actually override anything.

What does @override mean Code?

@Override means you are overriding the base class method. In java6, it also mean you are implementing a method from an interface. It protects you from typos when you think are overriding a method but you mistyped something. Follow this answer to receive notifications.

What is @override mean in Java?

The @ is Java Annotations. The @Override means that the method is overriding the parent class (in this case createSolver ). The Javadoc states for @Override : Indicates that a method declaration is intended to override a method declaration in a superclass.

Why do we put @override above the definition of some methods?

The @Override annotation allows other developers (and you, when you forget) to know that this method overrides something in a base class/interface, and it also allows the compiler to yell at you if you're not actually overriding anything in a base class.


1 Answers

@Override is a Java annotation. It tells the compiler that the following method overrides a method of its superclass. For instance, say you implement a Person class.

public class Person {    public final String firstName;    public final String lastName;     //some methods     @Override public boolean equals(Object other) {       ...    } } 

The person class has an equals() method. The equals method is already defined in Person's superclass Object. Therefore the above implementation of equals() is a redefinition of equals() for Persons. That is to say, Person overrides equals().

It is legal to override methods without explicitly annotating it. So what is the @Override annotation good for? What if you accidentally tried to override equals() that way:

public boolean equals(Person other) {    ... } 

The above case has a bug. You meant to override equals() but you didn't. Why? because the real equals() gets an Object as a parameter and your equals() gets a Person as a parameter. The compiler is not going to tell you about the bug because the compiler doesn't know you wanted to override. As far as the compiler can tell, you actually meant to overload equals(). But if you tried to override equals using the @Override annotation:

@Override public boolean equals(Person other) {    ... } 

Now the compiler knows that you have an error. You wanted to override but you didn't. So the reason to use the @Override annotation is to explicitly declare method overriding.

like image 76
snakile Avatar answered Sep 22 '22 15:09

snakile