aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class casting doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class casting doubt??" Watch "Class casting doubt??" New topic
Author

Class casting doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Guys,

The question below is from here

Suppose we have two classes defined as follows:

class Base extends Object implements Runnable
class Derived extends Base implements Observer



Given 2 variables created as follows:

Base base = new Base();
Derived derived = new Derived();



Which of the Java code fragments will compile and execute without errors?


1. Object obj = base;
Runnable run = obj;
2. Object obj = base;
Runnable run = (Runnable) obj;
3. Object obj = base;
Observer ob = (Observer) base;
4. Object obj = derived;
Observer ob2 = obj;

Answer:
2

Why can't 3 also be the correct option?? Why it gives a ClassCastException??

Thanks in advance.


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Shameer Subedar
Greenhorn

Joined: Apr 08, 2004
Posts: 22
You get the runtime exception because Base never implements the Observer interface. This won't complain at compile time as we are allowed to cast objects to any Interface type, this allows subclass of base to implement other interface(s) (if need)

However you can use the instanceof operator to check the runtime type of the object before you apply any cast.

Hope this helps,
Shameer
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Got it. So if instead of Observer, if it was a class, then it would give a compile time error.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
You can cast any object to an interface type, even if this class does not implement that interface. Because a subtype of this class could implement it (example with something).
"You can cast" in this context means, the compiler allows it, but you get a class cast exception.

There is one exception, when the class is final, compiler complains:




But something that works:

prints "iii in Achild"
Therefore the casting to all interfaces must be allowed from the compiler for all non-final classes.


Yours,
Bu.


all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Bu,

Thanks for the explanation. So final classes can't be cast to a reference type. Am I right??
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Bu,

Thanks for the explanation. So final classes can't be cast to a reference type of an interface. Am I right?? Or is it that final classes can't be cast to any other type?? Is that right?? Please confirm.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

Originally posted by Jothi Shankar Kumar Sankararaj:
Bu,

Thanks for the explanation. So final classes can't be cast to a reference type. Am I right??


Basically, what Bu said was... if a class is final, and doesn't support a particular interface, then the compiler can tell that a cast to that interface is not valid and complain. If the class is not final, then the compiler can't tell if a subclass of that class can implement that interface and hence, it will not complain.

This doesn't mean that the cast is valid. It just means that it can't tell you of the validity during compile time -- if the cast is not valid, it will complain at runtime.

Henry
[ December 19, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Interface references can be casted and assigned to class references with the assumption that any of the subclass object inheriting the our interface in the same hierarchy might be stored in the interface reference.

But the final classes can't be further inherited.
So Until unless the interface is implemented anywhere in the hierarchy till our final class they can't casted to the final class reference.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class casting doubt??
 
Similar Threads
instanceOf
type casting
Interface cast
casting
help me