• 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

cast

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Point { int x, y; }
public interface Colorable { void setColor(int color); }
public class ColoredPoint extends Point implements Colorable
{
int color;
public void setColor(int color) { this.color = color; }
}
class Test {
public static void main(String[] args) {
Point[] pa = new Point[100];
// The following line will throw a ClassCastException:
ColoredPoint[] cpa = (ColoredPoint[])pa;
System.out.println(cpa[0]);
int[] shortvec = new int[2];
Object o = shortvec;
// The following line will throw a ClassCastException:
Colorable c = (Colorable)o;
c.setColor(0);
}
}
how come
ColoredPoint[] cpa = (ColoredPoint[])pa;
generates a class cast exception ?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Point is the base class and ColoredPoint is the sub class. Although a ColoredPoint is a Point, a Point is not a ColoredPoint. In other words, you may cast a sub class to a base class, but you can not cast a base class to a subclass.
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your prompt reply dan.
i still don't get it, can anybody please explain this further ?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maria,
Let us take two classes Base and Sub. Sub is derived from Base. and we have two instances base, sub for them respectively. So,

base = (Base)sub ; //OK
sub = (Sub)base ; //not ok.
The reason is all the methods and data of base are visible to sub, but not the other way round.
so we can assign sub to base. But the Base class doen't know the details of Sub. So we can not assign it to sub.
HTH
Charu
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Important casting rules at runtime to remember here are
>> when superclass is explicitly cast to its subclass. it does not result in any compile time error but during run time results in ClassCastException.
>>An interface can be legally cast to a class only if the class implements the interface
the following code illustrates this:

-zarina
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maria,
Imagine that you go to a car dealership to buy a car. You look at two models: a base model that provides only minimal functionality and a special model that has air conditioning, a nice CD player, a global positioning system, etc. You prefer the special model, but you order the base model because the price is lower. A week later you go back to the dealer to pick up your new base model car but the dealer gives you the special model instead. Even though you were expecting a base model car, you are happy to accept the special model because it provides all of the functionality of the base model and it gives you some extra features that you like. On the other hand, if you had ordered the special model and the dealer tried to force you to accept the base model, you would refuse.
The Java Virtual Machine is like a good car dealer. If the customer is expecting the base model, the JVM will be happy to deliver the special model. However, if a customer is expecting the special model, the JVM won't try to give the customer the base model instead.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
References and the objects they refer to are different things. Casting is only used on object references. The runtime type of the object is unaffected, and dynamic method lookup is used.
Please note you can cast the object reference s1 (which is of type Super) to type Sub because the runtime type of the object referred to s1 is really of type Sub.

It goes like this:
Suppose I have two object references A and B and we have A = B;
If B is a subclass of A then this works and no explicit cast is necessary. This is called a widening conversion.
If A is a subclass of B then the object reference B must be explicitly cast to type A to avoid a compile time error. Furthermore, at runtime, the actual type of the object referred to by B is checked. Let's suppose it is C. If C is a subclass of A then no problem but if it's not then a ClassCastException will be thrown.
If neither of the above condition holds, as for example when A and B are of peer class types, then a compile time error will occur.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
For clear understanding read chapter 4 converting and casting of RHE. He has explained very clearly.
Thanks
praveen
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan, Charu, Praveen, Anthony and Zarina,
Thank you very much for responding... it's all clear to me now
Maria
 
reply
    Bookmark Topic Watch Topic
  • New Topic