In Java,@Override
is an annotation that checks and indicates that a method overrides the method in its parent class or interface.
1. What is @Override
definition
-
@Override
It is a type of Javaannotation, indicating that the methods in the subclass override the methods in the parent class or implement the methods in the interface. - It is mainly intended to enhance the readability and reliability of the code.
grammar
@Override public Return type Method name(Parameter list) { // Method implementation}
effect
-
Compiler verification:
- Compile-time checks whether the method has correctly rewrites the method in the parent class or interface.
- If the method signature does not match, the compiler will report an error.
-
Improve code readability:
- Clearly identify which methods are rewriting methods to facilitate code maintenance.
-
Avoid low-level errors:
- Avoid unexpected method overloading (Overload) due to typos or different parameter lists.
2. Usage of @Override
(1) Rewrite the parent class method
When a subclass overrides a method in the parent class, you can use@Override
Annotation.
Sample code
class Parent { public void display() { ("Preparent class method"); } } class Child extends Parent { @Override public void display() { //Rewrite correctly ("Subclass rewrite method"); } // @Override // public void Display() { // Compile error: method name error // } } public class OverrideExample { public static void main(String[] args) { Parent obj = new Child(); (); // Output: Subclass rewrite method } }
Things to note
- The method name, return type, and parameter list must be exactly the same as the method in the parent class.
- The access modifier for a subclass method cannot be stricter than the parent method.
(2) Implement interface method
When a class implements methods in an interface, it can also be used@Override
Annotation.
Sample code
interface Greeting { void sayHello(); } class Person implements Greeting { @Override public void sayHello() { // Correct implementation ("Hello!"); } } public class InterfaceExample { public static void main(String[] args) { Greeting person = new Person(); (); // Output: Hello! } }
Things to note
- The default method in the interface is
public abstract
, must be declared aspublic
。 - If omitted
@Override
Annotation, the code can still run, but lacks verification during the compilation period.
(3) Rewrite the Object class method
All classes in Java are inherited fromObject
class, its methods can be rewrite, for exampletoString()
、equals()
andhashCode()
。
Sample code
class Person { private String name; private int age; public Person(String name, int age) { = name; = age; } @Override public String toString() { // Rewrite the toString method return "Person{name='" + name + "', age=" + age + "}"; } } public class ObjectOverrideExample { public static void main(String[] args) { Person person = new Person("Alice", 25); (person); // Output: Person{name='Alice', age=25} } }
3. Common errors in @Override annotation
(1) Method name spelling error
If the subclass method does not match the name of the parent class method, it will not be considered a rewrite method.
Error Example
class Parent { public void display() {} } class Child extends Parent { @Override public void Display() { // Compilation error: The parent class method cannot be found } }
(2) Different parameter list
When the parameter list is inconsistent, the method is treated as an overload rather than a rewrite.
Error Example
class Parent { public void display(String msg) {} } class Child extends Parent { @Override public void display() { // Compilation error: method signature mismatch } }
(3) Modifier permissions are stricter
The access modifier for a subclass method cannot be stricter than the parent method. For example, the parent class method ispublic
, the subclass method cannot beprotected
orprivate
。
Error Example
class Parent { public void display() {} } class Child extends Parent { @Override protected void display() { // Compilation error: Access modifier is more stringent } }
4. The benefits of @Override
(1) Improve code readability
pass@Override
Annotation, other developers can clearly see which methods are rewritten.
(2) Compilation period verification
The compiler will automatically check whether the method correctly rewrites the methods in the parent class or interface to avoid low-level errors.
(3) Convenient code maintenance
When the method in the parent class or interface changes, use@Override
The method will report an error in time and remind the developer to modify the corresponding implementation.
5. @Override must be used?
(1) Optional but recommended
- If not used
@Override
Annotation, the rewrite method is still valid. - But omitted
@Override
It will lose the compiler's verification function, which can easily lead to unexpected errors (such as incorrect spelling of method names or inconsistent parameter lists).
Sample code
class Parent { public void display() {} } class Child extends Parent { public void display() { // @Override can be omitted, but it is not recommended } }
6. Limitations of @Override Annotation
(1) Applicable to methods only
-
@Override
It can only be used for methods, not fields or constructors.
Error Example
class Parent { public int value; } class Child extends Parent { @Override // Compile error: Not available for field public int value; }
(2) Unable to detect the implementation of the interface default method
- In the interface
default
When rewriting the method,@Override
Not mandatory.
7. Summary
Necessity of using @Override
Scene | Do you have to use @Override | reason |
---|---|---|
Subclass override parent class method | Recommended use | Improve code readability and avoid spelling errors |
Implementing interface method | Recommended use | Ensure that the implementation method complies with the interface definition |
RewriteObject Class Methods |
Highly recommended | Avoid invalid rewrites causing the default implementation to be called |
Field or constructor | not available |
@Override Applicable to methods only |
Advantages of @Override
- Improves the security of the code (compilation period check).
- Improve the readability of the code (clear the source of the method).
- Prevent low-level errors (such as spelling errors or signature mismatch).
In actual development, it is always recommended to add each rewrite method@Override
Annotation, this is a good programming habit.
Summarize
This is the end of this article about the usage of java annotation @Override. For more related java annotation @Override content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!