Java OCA OCP Practice Question 1039

Question

Consider the following classes:

class A  { 
      public int getCode (){ return 2;} 
} 

class MySubClass extends A  {  
  public void doStuff ()  {  
   }  
} 

Given the following two declarations, which of the options will compile?

A a = null; 
MySubClass aa = null; 

Select 4 options

A. a =  (MySubClass)aa; 
B. a = new MySubClass (); 
C. aa = new A (); 
D. aa =  (MySubClass) a; 
E. aa = a; 
F.  ((MySubClass)a).doStuff (); 


Correct Options are  : A B D F

Note

a is declared as a reference of class A and therefore, at run time, it is possible for a to point to an object of class MySubClass because A is a super class of MySubClass.

Hence, the compiler will not complain.

Although if a does not point to an object of class MySubClass at run time, a ClassCastException will be thrown.

A cast is required because the compiler needs to be assured that at run time a will point to an object of class MySubClass.

Once you cast a to MySubClass, you can call methods defined in MySubClass.

Of course, if a does not point to an object of class MySubClass at runtime, a ClassCastException will be thrown.




PreviousNext

Related