Website For Developers

Method overriding in Java



Syntax

@Override
methodName () {
}


Example of Method Overriding in Java

class Run {

   void show() {
     System.out.print( "People are running on the track." );
   }
}

class Walk extends Run {

   @Override // it is used when overriding a method in child class
   void show() {
     System.out.print( "People are walking in the ground." );
   }

   public static void main (String [] args) {
     Walk object = new Walk();
     object.show();
   }
}
Output

People are walking in the ground.

@Override annotation in Java