This question was asked to me in an interview. I have an overridden method in my sub-class. Using an instance of the subclass, I want to call the method of the super class. Bear in mind that the method is overridden.
The output of the following code is NO NO PRINT HI
.
What if I want to print Print Hello
using the object of overriding class? How do I do that?
class OverrideSuper {
public void printHello() {
System.out.println("Print Hello");
}
}
public class Overriding extends OverrideSuper {
public void printHello() {
System.out.println("NO NO PRINT HI");
}
public static void main(String[] args) {
//OverrideSuper obj1 = new OverrideSuper();
Overriding obj2 = new Overriding();
obj2.printHello();// this calls printHello() of class Overriding.
//I want to call printHello() of OverrideSuper using obj2. How do I do that???
}
}