Android @Override usage

65,971

Solution 1

It's an annotation that you can use to tell the compiler and your IDE that you intend the method that has that annotation to be an override of a super class method. They have warning/errors in case you make mistakes, for example if you intend to override a method but misspell it, if the annotation is there the IDE or the compiler will tell you that it is not in fact overriding the super class method and thus you can determine why and correct the misspelling.

This is all the more important for Android applications and activities for example, where all of the calls will be based on the activity lifecycle - and if you do not properly override the lifecycle methods they will never get called by the framework. Everything will compile fine, but your app will not work the way you intend it to. If you add the annotation, you'll get an error.

Solution 2

This code for the beginner who really want to understand about the @Override process, this will help you! (Remind inheritance concept of Java.)

For example, the Fish class might have two subclasses: FreshwaterFish and SaltwaterFish.

These subclasses would have all the features of the Fish class, but could further customize the objects through new attributes and behaviors or modified behaviors from the parent class Fish. For example, the FreshwaterFish class might include information about the type of freshwater environment lived in (e.g. river, lake, pond, or puddle).

Similarly, the SaltwaterFish class might customize the makeBabyFish() method such that the fish eats its mate after breeding (as defined in the super class) by using the override mechanism, like this:

public class SaltwaterFish extends Fish  
{  
    @Override  
    public void makeBabyFish(Fish fishSpouse, int numBabies) {  
        // call parent method  
        super.makeBabyFish(fishSpouse, numBabies);  
        // eat mate  
        eat(fishSpouse);  
    }  
}

Solution 3

The Override-Annotation is just a hint for the compiler that you want to overwrite a certain function. The compiler will then check parent-classes and interfaces if the function exists there. If not, you will get a compile-error.

Its basically just a safety mechanism.

For reference, see this article (override is explained somewhere in the middle)

Share:
65,971
Ian Vink
Author by

Ian Vink

https://mvp.microsoft.com/en-us/PublicProfile/5002789?fullName=Ian%20Vink

Updated on July 05, 2022

Comments

  • Ian Vink
    Ian Vink almost 2 years

    (Newbie to Java, old time C# guy.)

    I have noticed a lot of the use of @Override in Android example code. I thought that all Java methods were by default "Virtual"?

    What then does @Override do?

    Example:

    private class HelloWebViewClient extends WebViewClient {
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    
  • Romain Guy
    Romain Guy about 14 years
    It's not just for the IDE. The Java compiler will produce an error if you @Override a method that's not in a super class.
  • Flexicoder
    Flexicoder almost 11 years
    That link is dead now
  • McLan
    McLan over 10 years
    VERY WELL EXPLAINED .. thanks
  • the_prole
    the_prole about 10 years
    So basically you use override to "disinherit" and re-write a function from the super-class. Got it.
  • Matt
    Matt over 9 years
  • lostiniceland
    lostiniceland over 9 years
    Thanks for the updated link. I updated my answer.
  • erical
    erical about 8 years
    Why no class name added before eat method when calling it?
  • erical
    erical about 8 years
    and what if no "@Override" annotation, what will happen? some error?
  • Chaine
    Chaine about 7 years
    The best explanation ever
  • Trect
    Trect almost 5 years
    Whenever you get an error -> add @override. That is what the answer is saying -_-
  • Trect
    Trect almost 5 years
    This should be the accepted answer