What will be the output?
interface A{
public void method1();
}
class One implements A{
public void method1(){
System.out.println("Class One method1");
}
}
class Two extends One{
public void method1(){
System.out.println("Class Two method1");
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two();
a.method1();
}
}
interface A{
public void method1();
}
class One implements A{
public void method1(){
System.out.println("Class One method1");
}
}
class Two extends One{
public void method1(){
System.out.println("Class Two method1");
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two();
a.method1();
}
}
A. Class One method1
B. Class Two method1
C. Nothing will be printed
D. Compilation Error
Answer: Option B
Join The Discussion
Comments ( 1 )
What is method overriding in Java?
A. Redefining a superclass method in a subclass
B. Defining a new method with the same name in a subclass
C. Making a method private in a subclass
D. Hiding methods in a superclass
What is the purpose of method overloading in Java?
A. Creating static methods
B. Hiding methods in a superclass
C. Redefining methods in a subclass
D. Defining multiple methods with the same name but different parameters
A. @OverrideMethod
B. @OverrideSuper
C. @Override
D. @OverrideParent
What happens when a subclass tries to override a final method from the superclass in Java?
A. The final method is hidden
B. It results in a compilation error
C. The final method becomes static
D. The final method is hidden
as the child object is used to call the method, the method METHOD1 present in child class will be executed