File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference variable casting w.r.t interfaces

 
Rekha Srinath
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit..I think I am getting it. Will get back in case of further doubts.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic