开发者

The Meaning of @override in Android Studio [duplicate]

开发者 https://www.devze.com 2023-02-05 06:31 出处:网络
This q开发者_如何学Gouestion already has answers here: Android @Override usage [duplicate] (4 answers)
This q开发者_如何学Gouestion already has answers here: Android @Override usage [duplicate] (4 answers) When do you use Java's @Override annotation and why? (27 answers) Closed 3 years ago.

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


@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.


Its a Java annotation (not Android-specific). You use it to mean for this method to override a method.

The reason to use it is to catch errors when you create a method you intend to override a method, but through some error, it does not, e.g. typo in the method name, an error in the method signature, etc.. For example, sometimes developers to this:

class Foo { 
    public boolean equals(Foo other) {
       ...

The author intended this to override the superclass' equals method, but it does not (The parameter type should be Object). The program will compile fine, but will not use Foo.equals as the author intended.

class Foo {
    @Override
    public boolean equals(Foo other) {
       ...

The compile will now give an error because the method does not override another method. This points out the problem early and hopefully saves some debugging time tracking down the problem.


It's a Java annotation that tells the compiler that the method is intended to override a method from the superclass. It's not strictly necessary, but does help to catch errors at compile time.


@Override is an java annotation.

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

Also See

  • when-do-you-use-javas-override-annotation-and-why

Example:

class Parent{
    public void foo(){

    }
}

class Child extends Parent{
    //compile perfect !
    @Override
    public void foo(){

    }
    //will show compile time error message here as it is not being overridden 
    @Override
    public void foo(int i){

    }

}


It's an annotation in Java. It marks that the method is meant to override a method in a superclass.


That's a special, compiler reserved, Java annotation that denotes that you are overriding a superclass method.

0

精彩评论

暂无评论...
验证码 换一张
取 消