• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class casting doubt??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic