Method overriding allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.
/** * @author lautturi.com * Java example: Method Overriding example in java */ import java.util.*; class Animal { void show() { System.out.println("An Animal"); } } class Dog extends Animal { void show() { System.out.println("A Dog"); } } public class Lautturi { public static void main(String args[]) { Animal obj1 = new Animal(); obj1.show(); Dog obj2 = new Dog(); obj2.show(); Animal obj3 = new Dog(); obj3.show(); } }Source:www.lautturi.com
output:
An Animal A Dog A Dog