Method Overriding Example – Java Program

package com.javaproficiency;

class Y {

void display(){
System.out.println(” Y is called”);
}

}

public class X extends Y{

void display(){
System.out.println(” X is called”);
}

public static void main(String[] args) {

Y y = new Y();
X x = new X();
Y obj = new X();

y.display();
x.display();
obj.display();

}

}

Output:
 Y is called
 X is called
 X is called

5 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

− 2 = 2