Override parent class method in java with or without @ Override

  • 2021-09-24 22:26:28
  • OfStack

java override parent class method with or without @ Override

This is the problem I encountered when I was just learning java. I hope I can bring help to novice learners like me. Please correct me if there are mistakes. Thank you!


class Father{
    public String name;
    public int age;
    public Father(){
    }
    public void work(){
        System.out.println(" Build a house ");
    }
}
class Son extends Father{
    public int weight;
    public Son(){
    }
    public void work(){
        System.out.println(i);
    }
}

The controller types (including return value types, parameters, etc.) of the work () method of the face class and the work () method of the base class above are completely identical. In this case, it is OK to add @ Override or not, and the base class method can be overridden.

Because the java virtual machine automatically recognizes it when it is not written.


class Son extends Father{
    public int weight;
    public Son(){
    }
    public int work(int i){
        System.out.println();
    }
}

In this case, the work () method is considered by the system to be a newly defined method of the subclass, not inheriting the base class, which is also allowed.


class Son extends Father{
    public int weight;
    public Son(){
    }
    public int work(){
        System.out.println();
        return 1;
    }
}

However, this situation is not allowed when only the return value type is modified, and it is still regarded as an override of the base class. However, if the return value type changes, it will be considered by the editor that the base class overrides the return value type without 1 and report an error.

Therefore, it is reasonable to exist. If you need to rewrite the base class or add @ Override when writing code, you can also detect 1 errors that are easy to ignore.

The role of @ Override

@ Override is pseudo code, which means rewriting (of course, you can not write it), but writing it has the following advantages:

1. It can be used as a comment for easy reading

2. The compiler can verify whether the method names below @ Override are all in your parent class, and report an error if not.

For example, if you don't write @ Override and you write the wrong method name, your compiler can compile it because the compiler thinks this method is an added method in your subclass.

Example: When overriding the parent class's onCreate, adding @ Override system before the method can help you check the correctness of the method.


@Override
public void onCreate(Bundle savedInstanceState)
{ ... .}

This writing is correct, if you write:


@Override
public void oncreate(Bundle savedInstanceState)
{ ... .}

The compiler reports the following error:

The method oncreate(Bundle) of type HelloWorld must override or implement a supertype method

To make sure you override the onCreate method correctly (because oncreate should be onCreate).

If you don't add @ Override, the compiler will not detect an error, but will assume that you have defined a new method for the subclass: oncreate

What's the difference between @ override and @ override

1. Simply put, the @ override annotation tells the compiler that the following method is a method that overrides the parent class

2. If you don't write the @ override annotation to override the method directly, the compiler won't judge whether you have correctly overridden the method in the parent class.

If the parameter is different from the parent class when overriding the method, the program will not prompt for an error. This leaves a potential bug.

When you write the @ override annotation, the program will determine whether you have correctly overridden the corresponding method of the parent class.

And with this annotation, the program will automatically mask the methods of the parent class.


Related articles: