File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt" Watch "Doubt" New topic


Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362

Why does line 3 cause Class Cast Exception where as line 4 doesn't

[ April 06, 2004: Message edited by: Mary Cole ]
Ashok C. Mohan
Ranch Hand

Joined: Dec 03, 2003
Posts: 75
In line 3 u r trying to cast a base class object to a derived class,which causes the compile time error,as this does not make any sense.(Only derived class objects can be casted to base class objects.
In line 4 though u r trying to cast the interface reference to the base class,the underlying object to which the reference i is pointing is a base class object,so casting a base class object to itself does not cause a compile time error.

SCJP 1.4
Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment.
I agree. Here's the link:
subject: Doubt
It's not a secret anymore!