Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Devaka's Exam Lab Question

 
Mansi Agarwal
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Dont understand how the conversion. Please help..
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
First look at compile time evaluation of below statements




line 15: b=(C) m; this line has two castings 1) from m to C(Since m is of Interface type compiler skips check, it passes)
2) from C to b ( since variable b is of Type B which can refer to subclass C,this test passes as well)

work similarly for rest,,,,

second at runtime, you have to check for incompatible assignments, as show in below example


the above piece compiles fine but fails at run time
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more hint:

the below code compiles and runs fine..........
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm posting this for the third consecutive time. the question that deals with casting can be confusing and under pressure one can mark wrong answer.the following rules are really helpful i solving casting questions. they are derived from concepts only but are much faster to work with.

consider the generalize scenario

A B = (C) D

1. C and D should have relationship, either C is-a D or D is-a C, otherwise COMPILER ERROR. e.g Dog d = new Dog() ; String s = (String)d;

2. A and C should be of the same type of C should be subtype of A, otherwise COMPILER ERROR.

3. D should refer to a type that is same as of C or subtype of D, otherwise Runtime error.

also if D is null then it can be typecasted to anything(since null can be assigned to anything, except primitives) and the result will be null.

i recommend you follow the above rules and try to solve the question once more and definitely you will be able to crack it. also these are not rules/official, they have been derived from the common concepts that we know. if you are still not able to understand then please post it . we'll help you out
 
Mansi Agarwal
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Don and Gurpreet.

I will keep those points in my mind.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic