• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Regarding Casting between classes and interface

Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following example from (go4java : MockExam#2):

I am having some doubts which are as follows :
( I have numbered the code for more clarity )
Doubt 1:
1. Refering to line 8. Now when we do such a cast of an object
of class to an Interface (I1)a do we have to look at the declared type of object "a" or the runtime type which in this case is class A itself?
Let me classify it as -
Compile time check :

I am confused at this point ?? What will the compiler check
or how does it compile such code? On basis of the declared
type or the actual type. I think it is on basis of the declared type.
Run time check :

The object a should be object of class(or subclass) that implements the interface I1.In above case
it will give run-time ClassCastException as class A
does not implement interface I1.

Doubt 2 :
2. Uncomment the line 9. and line 10.
If we see the hierarchy of the classes then A and B
are no way related to each other.So once again let me
classify :
Compile time check :

As A and B are un-related the line 10. gives a compile time error.
So that means at compile time this hierarchy is checked. Fine.
But what is happening in the case when we cast to interface.
As from line 8. , the interface I1 and class A are not related
then why does it still compile ???

In summary , my doubts is that what should be check if we think that we are the human "Compiler" for such a casting to be done successfully.
Can someone explain to me.
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Angela,
This is a very interesting question.
Let take case by case and discuss about compile time

1. i1 = (I1) a; // compiles fine
Interface casting have to be handled carefully
You can cast ANY object to any interface reference
for ex:
Serializable s = (Serializable) a; //compiles fine too
Compiler will assume that the programmer does know what's he/she is talking or coding about regarding the above casting
But truth of the matter UNLESS "a" implements serializable JVM will throw classcast exception
So always make sure the following are ok with the compiler
Interface i = new A() // ONLY if A or its Parent implements i if //not casting is MANDATORY to compile
Object o = Anyinteface a; //compile likes it, becoz Object is the //superme power and that can reference //any interface/class

Case 2"
B b = new B(); // first uncomment this line
A a1 = (A) b; // if above line uncommented does not compile
Wheather you comment/uncomment they wont compile
It is becoz they are PEER classes and there is no correlation
between them
Only way to let it compile is
Either extend B from A
make a Object reference and refer it to object B
(Object o = (A) b; //Though (A) wont make much sense still it compiles

Hope it helps you


Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi both
The rules for checking casting at compile and runtime are given in JLS 5.5
Here are the rules that apply in this example:
" The remaining cases involve conversion between reference types. The detailed rules for compile-time correctness checking of a casting conversion of a value of compile-time reference type S (source) to a compile-time reference type T (target) are as follows:
"* If T is an interface type:
* If S is not a final class (�8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might).
* If S is a final class (�8.1.1), then S must implement T, or a compile-time error occurs.
"If a cast to a reference type is not a compile-time error, there are two cases:
* The cast can be determined to be correct at compile time. A cast from the compile-time type S to compile-time type T is correct at compile time if and only if S can be converted to T by assignment conversion (�5.2).
* The cast requires a run-time validity check

"* If R is an ordinary class (not an array class):
* If T is an interface type, then R must implement (�8.1.4) interface T, or a run-time exception is thrown.
hope helps
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic