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 class casting 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 "class casting" Watch "class casting" New topic
Author

class casting

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
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
}
}



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


the answer is one.......if anybody has understood kindly explain....Thanks in advance
Waclaw Borowiec
Greenhorn

Joined: Dec 14, 2009
Posts: 21
The answer is 1 and 4.
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
from where are you getting such interesting questions


well the answer should be 1
after applying is-a relationship you can do it
like B is-a A,E
C is-a A
D is-a B means D is also A,E

and when we perform a cast only the object is used not the reference
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
1 is answer becuse at runtime the object type is taken into consideration not the reference type, so at runtime jvm sees it as object D being casted to E and d implements E through its superclass so no exceptions will be there.


SCJP 1.6 96%
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62

At line 4--> we are casting object a with (E). a is actually D (at runtime). So a require casting as class D is subclass of class B which implements interface E.
At line 5 -->we are casting e with B because downcasting is required.

Answer should be 1.


Rajeev Kumar
SCJP 6.0 Certified 96%
Waclaw Borowiec
Greenhorn

Joined: Dec 14, 2009
Posts: 21
Neha Daga wrote:1 is answer becuse at runtime the object type is taken into consideration not the reference type, so at runtime jvm sees it as object D being casted to E and d implements E through its superclass so no exceptions will be there.


Answer 4 says that in line 4 the (E)a cast is required. And it is as the code doesn't compile without it (try it yourself). It shouldn't because in Java type safety is checked at compile time. Here we're trying to assign reference of type A to reference of type E. Class A doesn't implement interface E so this assignment requires explicit cast for the compiler. In runtime everything is fine - it's object of type D which is being cast to E, and D implicitly implements E, so no exception. That's why correct answers are 1 and 4.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class casting
 
Similar Threads
casting of objects
Doubt with conversion
Casting and Interfacses
interface questions
Cast expression