How to Use @Override in Java


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
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments