This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt with conversion" Watch "Doubt with conversion" New topic
Author

Doubt with conversion

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
source http://www.valoxo.ch/jr/mocks/mock01a.html Q7

Select two correct statements about the code given below?
class A{}
class B extends A implements E{}//line 1
class C extends A{}
class D extends B{}
interface E{}
public class Question07 {
public static void main(String[] args) {
A a = new D();//line 2
C c = new C();//line 3
E e = (E)a;//line 4
B b = (B)e;//line 5
}
}

A. The code compiles without error and runs fine.
B. Compilation error on line 1 because interface E is not yet declared (forward-referencing).
C. Compilation error on line 4 because class A does not implement interface E.
D. The cast on line 4 is mandatory.
E. The cast on line 5 is not mandatory.

Ans: A,D
Doubt: is line 4 valid as there is no relation between A and E. Also does the runtime class matters or the compile time?

pls help,
Gitesh
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Line 4 is OK. The cast is mandatory because the compiler doesn't know that the object does implement E, you do. It will run because you know that the instance at 'a' actually implements E. You would get an invalid class cast runtime exception if it doesn't.
By using the cast you are taking the responsibility of assuring that the cast is valid by having control of which object is instantiated there. The compiler can't guarantee it will be a valid cast, it only knows it is possible that the cast will be valid and you should make sure it is.


SCJP 1.5
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Greg you mentioned:

instance at 'a' actually implements E


But A does not implement E. It is D that implements E.

I'm still confused.


Gitesh
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
That's right, but you know that at 'a' you're holding a D, which is an A and an E too.
If A had implemented E in the first place, you wouldn't need a cast to make that assignment.
The compiler only knows that 'a' is just an A, but it can actually be an instance of a subclass that does implement the interface E.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
The reason it is allowed that its always possible to cast to interface type because even the class itself doesnt implement the interface a sub class might, with exception that the class itself is marked final.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Greg, does it mean that everything depends on runtime object, not the reference type.

Please correct me if I got it wrong.

Gitesh
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Well, "everything", if you mean what goes on here, you could put it that way... what you have to understand is that it is possible to hold an E with a reference type A, considering subclasses. That's why the compiler lets you do it. If at runtime that is not true, the cast will blow up your app.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt with conversion
 
Similar Threads
simple doubt with operators
Casting and Interfacses
Cast expression
class casting
Doubt with constructor overloading