For traditional programmers, override is the most important annotation. The annotations here refer to declarations that can only be used in methods,
She indicates that the annotated method is used to overwrite a declaration of the parent class. If you stick to this annotation, it can prevent illegal errors of a major category.
<span style="font-size:14px;">public class Bigram{private final char first;private final char second;public Bigram (char first ,char second){this.first=first;this.second=second;}public boolean equals(Bigram b){return b.first==first && b.second==second;}public int hashCode(){return 31*first+second;}public static void main (String args[]){.....s.add(new Bigram(ch,ch));}}</span>
This Code does not get the expected result because the bigram class creator originally wanted to overwrite the equals method and overwrite the hashcode method.
But unfortunately, the equals method is overloaded. (The equals parameter must be passed into the object class.) This equals method tests the object's identity, just like =
The operators are the same. This is why you cannot get the desired result.
If we add override to the Code to reload the parent class method identifier, this type of error can be avoided.
In the current IDE, the override annotation check function is provided, which allows you to find that this method is faulty during compilation.
<span style="font-size:14px;">public boolean equals(Bigram b){return b.first==first && b.second==second;}</span>
In short, remember to use the override annotation, the compiler will help you prevent a large number of errors, but there is an exception. It is not necessary to mark a specific class
Are you sure you want to override the abstract method declaration method.