• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Devaka's Exam Lab Question

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Dont understand how the conversion. Please help..
 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more hint:

the below code compiles and runs fine..........
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Don and Gurpreet.

I will keep those points in my mind.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic