• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class casting doubt??

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shameer Subedar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. So if instead of Observer, if it was a class, then it would give a compile time error.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bu,

Thanks for the explanation. So final classes can't be cast to a reference type. Am I right??
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic