A

Admin • 802.91K Points
Coach

Q. Determine output:
class A{
public void method1(){
System.out.print("Class A method1");
}
}
class B extends A{
public void method2(){
System.out.print("Class B method2");
}
}
class C extends B{
public void method2(){
System.out.print("Class C method2");
}
public void method3(){
System.out.print("Class C method3");
}
}
public class Test{
public static void main(String args[]){
A a = new A();
C c = new C();
c.method2();
a = c;
a.method3();
}
}

  • (A) Class B method2 Class C method3
  • (B) Class C method2 Class C method3
  • (C) Compilation Error
  • (D) Compilation Error D. Runtime exception
  • Correct Answer - Option(C)
  • Views: 8
  • Filed under category Java
  • Hashtags:

Explanation by: Admin
It is important to understand that it is the type of reference variable - not the type of the object that it refers to - that which determines what members can be accessed. That is, when a reference to a subclass object is assigned to a super class reference variable, we will have access only to those parts of the object defined by the superclass.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.