• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Casting problem

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a new doubt about casting. I don' t understand if it's illegal to cast the father class to child class, a class to an interface and an interface to a class (suppose class implements interface). I don't understand the general rule so don't understand why following code compiles ad runs successfully:




Please help me to understand the reasoning behind thi code from line 16 to line 19.
tks

bye
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any Interface can be cast to any non final Class type eventhough the class did not implement the interface also if that interface points to any object that could not be castable then you will get runtime exception.

E.g. Line 16.Class A did not implement the interface Min but still casting is allowed.


SO in this case m is the reference of type interface which is not pointing to any object so that it can be cast to any class type.

Also you know Superclass reference can point to any subclass object but not vice versa and you need a cast to sub type if you are down casting it.

In your case A can point to B without any cast because A is a superclass of B.But if you want to do it other way then you have to cast like

B b=(B)a; //assuming a is of type A

If the reference "a' points to any object that is not B or sub type of B then you will get runtime exception.


 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My approach to see if the code compiles is:

1. determine the class relationships:
B is an A,
C is a B and is an A
B is a Min
C is a Min
So:


2. look at the reference declarations only


3. check each assignment:


For runtime, you need to check the actual object the references refers to (what's behind the new operator?).
If the object type inherits or implements the assigned type, it succeed without explicit cast;
If the object type is a super type of the assiged type, you need an explict cast, otherwise exception raises.
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siva Masilamani wrote:Any Interface can be cast to any non final Class type eventhough the class did not implement the interface


WHY? This blows my mind off. Ofcourse, you are right. An interface can be cast to another class which does NOT implement it. But that makes no sense. That interface object can NEVER be an object of that class, so why does Java compiler allow it, and why no runtime ClassCastException?

Please take a look at the following code:


This prints the following:
m1 instanceof Diamond: FALSE

This makes no sense whatsoever.
Firstly, why allow this type of casting (line 1) ???
Secondly, how can a variable d1, declared as a type of class Diamond and cast to such, is NOT an instanceof Diamond???

Any enlightenment?
 
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

Nidhi Sar wrote:
Any enlightenment?



Try...



Notice that the object IS-A Diamond, and IS-A Moldable.

Henry
 
Sheriff
Posts: 9708
43
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

That interface object can NEVER be an object of that class


Nope, the cast/instanceof comparison might be successful at run time

You see, the cast/instanceof comparison is allowed by the compiler because there might be a class somewhere that extends that class and that interface. This is why it is allowed. But if the class C is final, then we know that there can't be a sub-class of class C, so the cast/instanceof comparison will NEVER succeed in that case and the compiler will give an error...

[Henry posted before me ]
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, great explanation.

@ PlasticDiamond !!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
infact , you are not creating any object here[interface is a specification].

 
Tony King
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thank you for your replies.

Now I should have understood why my code is good for compiler but i don't understand why there are not runtime error at line 18 (Min m2 = (C) b2;).

What is the type of b2 object (not reference) at line 18?

If I change this line with:

it gives me runtime error.. why? Is "general rule" that I cannot cast a father object (like B) to child object (like c) or vice versa?
 
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

Tony King wrote:
Now I should have understood why my code is good for compiler but i don't understand why there are not runtime error at line 18 (Min m2 = (C) b2;).

What is the type of b2 object (not reference) at line 18?



If you follow your program, you will see that b2 had the value of null, prior to line 18. A null can be casted to any class type.

Henry
 
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

Tony King wrote:
If I change this line with:

it gives me runtime error.. why?



At runtime, you can't cast an object to some type that it is not. In this case, a B instance is not a C type.

Henry
 
zhong chen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony,

Henry is right. You can always follow this practice to determine whether a ClassCastException will throw or not. To be more specific, what object on the heap does b2 point to? You can do a reverse tracing:
before line 18:
b2 --> a2
a2 --> m
m --> b
b --> m
m = null

You see what b2 currently points to is null. You can assign null to any non-primitive type. The point here is you need to trace back to the most resent new operator or null assignment. If the type behind new operator is not something that can be assigned to Min m2, ClassCastException will throw.

 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic