Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

@Override Annotations usage in Java Programming Language

What are Annotations in Java ?.

Annotations in Java are metadatas that adds information to your code in a formal way so that you can keep track to the information added at a later stage. They provide a Java programmer information which describes the program fully in a sophisticated way. Annotations help us in generating descriptor files and giving us way to write boilerplate code each time.

Annonations in Java have a symbol @ in its syntax declaration. Java contains general purpose built in annotations defined in java.lang package as :

1. @Override : This annotation is used over method which we intend to override in sub-class. It generates compile-time error if we accidentally give method signature incorrect at the time of overriding.


Program to demonstrate how to use @Override Annotation in Java

package com.hubberspot.examples;

class Shape {
 
 public void draw() {
  System.out.println("Shape draw()");
 }
 
 public void name(){
  System.out.println("Shape name()");
 }
}

public class Triangle extends Shape {
 
 @Override
 public void draw() {
  System.out.println("Triangle draw()");
 }
 
 @Override
 public void name() {
  System.out.println("Triangle name()");
 }
 
// Compiler throws an error Triangle must
// override or implement supertype method
 @Override
 public void draw(int a) {
  System.out.println("Triangle draw()");
 }

}

 
© 2021 Learn Java by Examples Template by Hubberspot