R

Ritu Singhal • 4.36K Points
Extraordinary

Q. What will be the output?
class A{
static void method(){
System.out.println("Class A method");
}
}
class B extends A{
static void method(){
System.out.println("Class B method");
}
}
public class Test{
public static void main(String args[]){
A a = new B();
a.method();
}
}

  • (A) Class A method
  • (B) Class B method
  • (C) Compilation Error
  • (D) Compilation Error D. Runtime Error
  • Correct Answer - Option(A)
  • Views: 34
  • Filed under category JAVA

Explanation by: Ritu Singhal
Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type. But in the above case the methods are static which means access to them is always resolved during compile time only using the compile time type information. Accessing them using object references is just an extra liberty given by the designers of Java.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.


Question analytics