Home Tutorial Java Scjp Part6 SCJP Module-6 Question-12

 
 

SCJP Module-6 Question-12
Posted on: July 14, 2010 at 12:00 AM
The program given below will test your understanding of Inheritance in Java and it will also test typecasting of objects from one class to another class.

Given below the sample code :

1 interface zoo {}
2 class A implements zoo {}
3 class B extends A{}
4 class D extends B{
5 public static void main( String[] args ) {
6 B x = new B();
7 // what code should be here
8 }
9 }

What code should be inserted at line 7 to throw " java.lang.ClassCastException " implicitly ?

1. A a = x;
2. zoo z = (D)x;
3. zoo z = (A)x;
4. B b = (B)(Alpha)x;

Answer :

(2)

Explanation :

B(super class of D)cannot be cast to D(subclass of B).

Related Tags for SCJP Module-6 Question-12: