Android @Override usage

JavaAndroidOverriding

Java Problem Overview


(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;
    }
}

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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)

Solution 4 - Java

Override is mostly used in case of defining a method.Overriding is similar to way its meaning.

I will try to explain in very lame way.Suppose if you have Oncreate() Method already defined and have the associated properties with it. and Again when you call Oncreate() method in your code for certain object,the code which you have written now... will override the formally defined property or inherited property of Oncreate() for your application.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIan VinkView Question on Stackoverflow
Solution 1 - JavaJRLView Answer on Stackoverflow
Solution 2 - Javasamir voraView Answer on Stackoverflow
Solution 3 - JavalostinicelandView Answer on Stackoverflow
Solution 4 - JavavivekView Answer on Stackoverflow