Share on Google+Share on Google+

SCJP Module-6 Question-12

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 :


Explanation :

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


Posted on: July 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null