BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

@Override annotation in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

@Override annotation is used when we override a method in sub class. Generally novice developers overlook this feature as it is not mandatory to use this annotation while overriding the method. Here we will discuss why we should use @Override annotation and why it is considered as a best practice in java coding.

Lets take an example first to understand how it is used then we will discuss it in detail:

Example

class ParentClass
{
	public void displayMethod(String msg){
		System.out.println(msg);
	}
}
class SubClass extends ParentClass
{
	@Override
	public void displayMethod(String msg){
		System.out.println("Message is: "+ msg);
	}
	public static void main(String args[]){
		SubClass obj = new SubClass();
		obj.displayMethod("Hey!!");
	}
}

In the above example we are overriding a method displaymethod() in the child class. Even if we don’t use the @Override annotation, the program would still run fine without any issues, You would be wondering the why do we use this annotation at all. Lets discuss about it:

Why we use @Override annotation

Using @Override annotation while overriding a method is considered as a best practice for coding in java because of the following two advantages:

1) If programmer makes any mistake such as wrong method name, wrong parameter types while overriding, you would get a compile time error. As by using this annotation you instruct compiler that you are overriding this method. If you don’t use the annotation then the sub class method would behave as a new method (not the overriding method) in sub class.

2) It improves the readability of the code. So if you change the signature of overridden method then all the sub classes that overrides the particular method would throw a compilation error, which would eventually help you to change the signature in the sub classes. If you have lots of classes in your application then this annotation would really help you to identify the classes that require changes when you change the signature of a method.

Top Related Articles:

  1. Java Enum Tutorial with examples
  2. Final Keyword In Java – Final variable, Method and Class
  3. How to write to file in Java using BufferedWriter
  4. Constructor Overloading in Java with examples
  5. Java – Static Class, Block, Methods and Variables

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Datt says

    November 3, 2016 at 4:48 AM

    Hi Chaitanya, I would require a clarification from you with respect to the override annotation. You have above quoted as “If you don’t use the annotation then the sub class method would behave as a new method (not the overriding method) in sub class”. Because the subclass is already extending the parent class, ideally (without providing annotation) the method however will get overriden as per my understanding when it has implementation. Based on this, Can you please explain me the point sub-class method behaving as new method.

    Reply
  2. Raj says

    January 13, 2017 at 11:05 AM

    Hi Datt, I guess Chaitanya meant : “When a programmer makes any mistake such as wrong method name, wrong parameter types while overriding and if annotation is not used, programmer won’t know as the compile time error won’t be thrown. And the sub class method would behave as a new method (not the overriding method) in sub class”

    Reply
  3. suyogya says

    October 28, 2018 at 10:04 PM

    If you change displayMethod(String msg) to displayMethod(String msg, String msg2), program gives error of you have @override. If you comment @override program doesn’t give any error and prints as per displayMethod of SubClass.

    Reply
    • Chaitanya Singh says

      October 29, 2018 at 1:40 PM

      This is because when you change the signature to displayMethod(String msg, String msg2), you are not overriding the parent class method. @override annotation is used when you are overriding the method in the child class, this is why you get the error. When you remove the annotation, the displayMethod(String msg, String msg2) method is treated as a normal (not overriden) method of child class and you don’t get error. In short “If you use @override annotation on a method that you are not overriding, you will get an error”. For more information, refer this guide: Overriding in Java.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap