I am completely new to Android Studio and I want to know the purpose of the @Override statement in Android Studio.
I am completely new to Android Studio and I want to know the purpose
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
@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.
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:
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:
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.