In this example we will show how to use @Override annotation to override a method in Java.
Source Code
package com.beginner.examples;
public class OverrideExample {
static class A {
public void test(){
System.out.println("A");
}
}
static class B extends A{
@Override
public void test(){
System.out.println("B");
}
}
public static void main(String[] args) {
B b = new B();
// cast b to A
A a = (A)b;
a.test();
}
}
Output:
B