• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Reference variable casting w.r.t interfaces

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Get Certified Exam Lab / Practice Exam 1, Q No. 29
Which of the lines cause exception?
Answer: None of the line cause exception.



I am able to understand till line 16.

But in line 17, there is a casting done on m with reference variable of type A. My doubt is: When there is no relation between A and Min, how is this reference variable casting working? What are the rules behind this? And beyond line 17 too, I am getting confused

I am having a tough time understanding this. If anyone could explain this concept (casting with respect to interfaces specifically) in a simple way, I would be pleased.

Thanks in advance.
[ November 20, 2008: Message edited by: Rekha Srinath ]
 
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well line 17 works as A and Min have a common sub type B. So the object represented by m might point to an object of class B this is why the assignment worked. Even the cast is not neccesary. If however m points to an object of type other than B, then there will be a ClassCastException at runtime but in the question we are concerned about compilation only. This problem that I posted when I first came to javaranch is also a similar problem but it is related to instanceof...
[ November 20, 2008: Message edited by: Ankit Garg ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit..I think I am getting it. Will get back in case of further doubts.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic