This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes cast Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "cast" Watch "cast" New topic


Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
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;
int[] shortvec = new int[2];
Object o = shortvec;
// The following line will throw a ClassCastException:
Colorable c = (Colorable)o;
how come
ColoredPoint[] cpa = (ColoredPoint[])pa;
generates a class cast exception ?

SCJP 1.4
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
thanks for your prompt reply dan.
i still don't get it, can anybody please explain this further ?
Charu Murali
Ranch Hand

Joined: Jul 12, 2002
Posts: 37
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.
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
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:

Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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.
Kumar Kausikasa
Ranch Hand

Joined: Jun 04, 2002
Posts: 45
Hi ,
For clear understanding read chapter 4 converting and casting of RHE. He has explained very clearly.
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
Dan, Charu, Praveen, Anthony and Zarina,
Thank you very much for responding... it's all clear to me now
It is sorta covered in the JavaRanch Style Guide.
subject: cast
Similar Threads
Object[] to Long[]
Doubt about ArrayStoreException
Assigning/Casting Array types to objects