File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCastException ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCastException ?" Watch "ClassCastException ?" New topic
Author

ClassCastException ?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
Upcast don't need any casts. fine.
but downcasts do. but for superclass-subclass case the runtime gives the castexception.
well then what is the point of allowing such a cast because it is going to throw an exception.

say Sub extends Super
Super sup = new Super();
Sub sub = new Sub();
sub = (Sub)sup; compile is ok but runtime gives the exception.
then what is point in providing such a cast. after all one cannot run this program, so might as well stop it right away at compile time. Yes i understand that there are some errors that are bound to happen at runtime. but this one is deliberate. the language rules themselves have said, hey let's make it ok while compile but not at runtime.
Point is , then might as well give the user a message that this is not correct. hope i could convey my point. (asking this as i have a feeling that i am missing something here)
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi mark,
this casting thing is not for "compile time" known things. here u know that sup and sub objects are actually different as u allocated memory to them using appropriate constructors of the classes Super and Sub.
this casting helps in runtime type detection. as e.g. if you have Super and Sub but u have something like,
Super sup = new Sub();
Sub sub = (Sub)sup;
this is essentiall useful in interfaces. say u have an interface Product and two classes namely, Shoe and Socks implementing that interface. now if u wanna have Vendor class that has a method to know price for each of the items available then that method can just invoke method on appropriate Shoe or Socks object by the following prototype,
class Vendor {

float getPrice(Product p) {
p.getPrice();
}
}
where getPrice is a method of interface Product. here, i can pass any (Shoe or Socks) object as argument and i would get appropriate price.
Shoe sh = new Shoe();
Socks sc = new Socks();
Vendor v = new Vendor();
v.getPrice(sh); (1)
v.getPrice(sc); (2)
it is same as casting sub class object into super type when the "sh" or "sc" argument is passed to the getPrice() method at (1) or (2) as the interface Product is super class (if u want to say so) of Shoe and Socks. but this detection is RUN TIME and not compile time and as "sh" and "sc" objects implement Product interface this works if they don't then it will give ur Runtime class cast exception as u expect.
here, the ease is that Vendor class need only one method getPrice() to delegate to appropriate item object rather than two diff methods for Shoe and Socks type of objects.

well, i'm not very good at explaining but i tried my best.
hope u got it.
regards
maulin.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Originally posted by mark stone:
then what is point in providing such a cast. after all one cannot run this program, so might as well stop it right away at compile time. Yes i understand that there are some errors that are bound to happen at runtime. but this one is deliberate. the language rules themselves have said, hey let's make it ok while compile but not at runtime.
Point is , then might as well give the user a message that this is not correct. hope i could convey my point. (asking this as i have a feeling that i am missing something here)

Yes, you are forgetting about inheritance and polymorphism. These two mechanisms allow us to program to interfaces rather than implementations. By doing so, you can write generic code that uses classes that you don't even know (or care) existed.
It is this ability to write generic code that makes it impossible (and undesirable) to do all checking up front during compile time. (Well, that and our inability write psychic compilers that know what we really meant to write ) We need to be able to defer some of the checking until runtime, when we know for certain the specific types of objects being used.
Just think of the cast as your way of telling the Java compiler "OK, I know that you think this reference is an X but it really is a Y. Trust me, I know what I'm doing." The compiler takes your word for it with the implied reply "OK, but you'd better be sure because Runtime will complain if it's not as you say it is."
Take for example the Collection API classes and interfaces in the java.util package. Let's look at the List interface and one of its concrete classes, Vector, in particular.
You can use a Vector to store any object with the add() method. However, since the Vector class was written way before you came along, the author didn't know about (or cared) that you would use Vector to store your own classes. So, the get() method was written generically to return an Object.
Enter you, the application programmer and user of Vector. You write specific code. When you add an Object to Vector, you probably know what its class is. The Vector doesn't know or care: all it knows is that this thing you are adding is an Object. When it's time to get back the object, Vector does so knowing only that whatever it is giving back is an Object. Here's some possible code:

So, now that you've gotten back an Object (at 1), you need to do something specific with it. At this point the compiler, which really isn't as bright as you think it is , has forgotten what you did before (at 0) and needs your assurance (the cast) that everything will be OK during runtime before it will accept the statement at 3.
Notice that I left 2 blank. I program defensively and I usually add safeguarding code to guard against stupid mistakes I may make down the road. The safeguarding code that you would insert at 2 above would be something like this:

This still throws an exception but it's a RuntimeException and these are the kinds you want to find during testing so that you can fix logic errors.
HTH,


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException ?