Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Downcast objects

 
Ranch Hand
Posts: 68
MyEclipse IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3. public class Redwood extends Tree {
4. public static void main(String[] args) {
5. new Redwood().go();
6. }
7. void go() {
8. go2(new Tree(), new Redwood());
9. go2((Redwood) new Tree(), new Redwood());
10. }
11. void go2(Tree t1, Redwood r1) {
12. Redwood r2 = (Redwood)t1;
13. Tree t2 = (Tree)r1;
14. }
15. }
16. class Tree { }

I suppose, the error lines are 5,8,12 to throw an exc. Can you explain why, line 12 Redwood---> t1 is a downcast, but should work?
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the correct answer for this question? And where this question from?
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what is an 'exc'?

Please write correct and full words while posting and if you're quoting an author in your post, please mention the book and the author in your post. Please note that the latter is not optional.

Chan.
 
timo corn
Ranch Hand
Posts: 68
MyEclipse IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, question taken from kb book, chaper 2 selftest, exc is exception and the correct answer is the one I have written.

I think this is the solution:


Tree t = new Tree( ) can never be downcast to a Redwood.
Tree tr = new Redwood( ) can be downcast to a Redwood.

See why?

Remember, the object never changes, but it's "references" do.

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

timo corn wrote:
I suppose, the error lines are 5,8,12 to throw an exc. Can you explain why, line 12 Redwood---> t1 is a downcast, but should work?



We need put Java code within code tags and indent it well so it is readable. See UseCodeTags (Click).

Now for your answer--
A Redwood is-a Tree. And the first argument of go2 takes a Tree. So you could pass it a Redwood also - the thing is the compiler does not know what object you are passing. All it knows is it is a Tree ( which includes Redwood also ). When you do the cast in line number 12, the compiler trusts that you passed it a Redwood as the first argument ( which you did not but the Compiler doesn't know -- it trusts that you know what you are doing ) and allows the cast.

The JVM has full knowledge of what was passed to go2 and hence the JVM doesn't allow it. Hence that should give you a ClassCastException at runtime.

Does that help?
Chan.
 
This tiny ad is wafer thin:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic