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

Reference casting question

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IN the ExamLab there is one question having following casts:

Min (in interface) m = null;
b=(C)m;
m=(Min)b;
A a2=(A)m;
B b2=(B)a2;


.... all these bunch of casting will have no problem in runtime (they have passed the compiling time verification) because m is null at the first place right?

thanks
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of requests here 'Zhen Zhen'

1. Users are required to follow a naming policy -> http://www.javaranch.com/name.jsp Please change your name

2. Post the class relationship details, without which this question is not answerable.

3. Use CODE tags to display your code, if you wish to edit this post later.

Thanks for understanding
 
Jennifer Zhen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:A couple of requests here 'Zhen Zhen'

1. Users are required to follow a naming policy -> http://www.javaranch.com/name.jsp Please change your name

2. Post the class relationship details, without which this question is not answerable.

3. Use CODE tags to display your code, if you wish to edit this post later.

Thanks for understanding



Hi D. B.

I already explained to one of your admins about my name. I don't know why you ask again. You might want to ask him/her. But for your convenience, [DELETED].

Thanks for reminding me the details, I think this is now detailed enough. If someone could answer that, it would be great, if now, let it be.

For using CODE, I know there is such a thing (how can someone forget about it --- doesn't it look "cool"???) but I didn't know JavaRanch is imposing it as a rule. Is it? for someone who can even misunderstand such a simple piece of code?



[HENRY: Deleted statement... Regardless, thanks for the change]
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[HENRY: DELETED -- See below]
 
Greenhorn
Posts: 10
Netbeans IDE VI Editor PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Zhen

I remember that question from one of the mock exams because I got it wrong the fist time, too.
Truth is, when casting to classes, the compiler checks if the casted variable and the class it should
cast to are in the same Class Hierarchy i.e. if it is theoretically possible that it could work. Trying
to cast "(ArrayList)x" where x is a String would give a compiler error.

But Interfaces are different. My guess is that as classes can implement multiple Interfaces the compiler just gives up trying to be clever here so
you can cast to whatever interface you want - if it does not match at runtime you get the (unchecked) ClassCastException.

As you can assign null to every reference variable it makes sense that you can also cast it to whatever you want.

bye
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But Interfaces are different. My guess is that as classes can implement multiple Interfaces the compiler just gives up trying to be clever here so you can cast to whatever interface you want - if it does not match at runtime you get the (unchecked) ClassCastException.



Actually, the compiler is not "giving up trying to be clever", it really can't tell.

For example, let's say you have a class A, that does *not* implement an interface I. The compiler can't derive anything from that fact. This is because it is possible for class B to extend A and implement I. since, class B IS-A A, and implements I, technically, you should be able to cast between A and I.

Now... if class A was a final class, meaning that you can't extend it, then the compiler will complain if you try to cast between A and I.

Henry
 
Jennifer Zhen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[HENRY: DELETED -- because this is in response to a deleted post]
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We appreciate it very much when users help us with the moderation of topics -- in fact, we are very grateful for it. It makes our job so much easier (and nicer).

However, I would like to remind everyone, that the "be nice" rule still apply. I am not saying that it was violated, but nonetheless, I am nipping this side discussion in the bud.


"Jennifer Zhen", I have sent you a private message.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic