Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the @Override
annotation is used to indicate that a method in a subclass is intended to override a method with the same signature in its superclass. This annotation helps to catch errors at compile-time, ensuring that the overriding method has the correct signature and is properly overriding the superclass method. In this tutorial, we will cover the basics of the @Override
annotation and how to use it effectively.
When you want to override a method in a subclass, use the @Override
annotation just before the method definition to indicate that the method is intended to override a method in the superclass.
Example:
class Animal { public void makeSound() { System.out.println("Some sound..."); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("Woof!"); } }
In the example above, the Dog
class extends the Animal
class, and the makeSound()
method in the Dog
class is marked with the @Override
annotation to indicate that it is intended to override the makeSound()
method in the Animal
class.
The @Override
annotation provides the following benefits:
Compile-time checking: If a method marked with the @Override
annotation does not actually override a method in the superclass, the Java compiler will produce an error. This helps to catch mistakes like misspelling the method name or using the wrong method signature.
Code readability: The @Override
annotation makes it clear that a method is intended to override a superclass method, which can improve code readability and make it easier to understand the intention of the developer.
The @Override
annotation can also be used when implementing methods from an interface. This ensures that you are correctly implementing the interface method and that your method signature matches the one defined in the interface.
Example:
interface Greeting { void sayHello(); } class EnglishGreeting implements Greeting { @Override public void sayHello() { System.out.println("Hello!"); } }
In this tutorial, we discussed the basics of the @Override
annotation in Java and its benefits. Using the @Override
annotation is a good practice when overriding methods in a subclass or implementing interface methods, as it helps to catch errors at compile-time and improves code readability.
Using @Override
in Java Methods:
@Override
annotation is used to indicate that a method in a subclass is intended to override a method in its superclass.public class Parent { public void doSomething() { // Parent implementation } } public class Child extends Parent { @Override public void doSomething() { // Child overrides the parent method } }
When to Use @Override
in Java:
@Override
when you intend to override a method from a superclass. It helps catch errors at compile time if the method isn't actually overriding anything.@Override public void doSomething() { // Child overrides the parent method }
Java @Override
Annotation Rules:
@Override
annotation must be used only for methods that are intended to override a method in a superclass. It helps enforce correct method overriding.@Override public void doSomething() { // Child overrides the parent method }
Override Annotation in Java Inheritance:
@Override
annotation ensures that the method in the subclass is indeed intended to override a method in the superclass.@Override public void doSomething() { // Child overrides the parent method }
Java @Override
vs @Overload
:
@Override
is used for method overriding, whereas @Overload
(not a standard annotation) would be used for method overloading, i.e., having multiple methods with the same name but different parameter lists.// Method Overloading (not using @Overload) public void doSomething(int x) { /*...*/ } public void doSomething(String str) { /*...*/ }
Purpose of @Override
in Java:
@Override public void doSomething() { // Child overrides the parent method }
Benefits of Using @Override
in Java:
@Override public void doSomething() { // Child overrides the parent method }
Common Mistakes with @Override
Annotation:
@Override
, such as using it on a method that is not actually overriding a superclass method.// Incorrect usage @Override public void doSomething() { // This method does not override any superclass method }
Java @Override
and Method Signature:
@Override
must have the same method signature as the method in the superclass, including the return type.@Override public void doSomething() { // Correct method signature }
Checking @Override
Correctness in Java:
@Override
is indeed overriding a method in the superclass. Any mismatch results in a compilation error.@Override public void doSomething() { // Correct method signature }
Java @Override
and Method Visibility:
public class Parent { protected void doSomething() { // Parent implementation } } public class Child extends Parent { @Override public void doSomething() { // Child overrides the parent method with the same or wider visibility } }
Java @Override
and Interface Implementation:
@Override
is also used when implementing methods from an interface, ensuring that the annotated method is indeed intended to fulfill the interface contract.public interface MyInterface { void doSomething(); } public class MyClass implements MyInterface { @Override public void doSomething() { // Implements the interface method } }
Using @Override
with Java 8 Default Methods:
@Override
is used to explicitly indicate that the method is intentionally overriding a default method from the interface.public interface MyInterface { default void doSomething() { // Default implementation } } public class MyClass implements MyInterface { @Override public void doSomething() { // Overrides the default method } }