Aslam a
Aslam a

Reputation: 759

java.lang.ClassCastException compile time check could have been handled better way

I have two classes parent and child, clascastexception occurs in runtime for the below code given.

From the below code its obvious that there will be classcastexception. I am thinking that java could have handled in betterway so that in the below case it could have given compile time error.

I is there any other reason that java is not giving error during compile time. Just want to know if anything i am missing.

public class ClassCastExcp {

    public static void main(String args[]){
        Child chd = (Child) new Parent(); //it will force you to cast and gives classcastexception in runtime
    }

}

class Parent{
}

class Child extends Parent{
}

Upvotes: 1

Views: 110

Answers (4)

Stephen C
Stephen C

Reputation: 718826

Based on what you wrote, I take it that you fully understand why you get the runtime exception. (If not, see the other answers.)

Is there any reason that java is not giving error during compile time. Just want to know if anything I am missing.

It is not giving a compilation error because the code is legal Java according to the JLS. (See @ajb's answer for more details, but the real explanation is in the JLS itself.) A compliant Java compiler is required to accept this ... at least in "compliant" mode.

Now, a Java compiler is allowed to report a warning for something like this, and it is also allowed to escalate warnings to errors. However:

  • This particular example is pretty unlikely in real code. Compiler writers typically generally don't go to the bother of implementing warnings for mistakes that occur rarely.

  • Anyone who implemented a Java compiler that treated this kind of thing an error by default would get a lot of flak from users who have to deal with code written by other people / organizations.

Upvotes: 3

ajb
ajb

Reputation: 31689

This sentence in JLS 15.3 is important:

In other words, the value of an expression whose type is T is always suitable for assignment to a variable of type T.

This means that if an expression has class type T, then at run time, the actual value could be any type that could be assigned to a variable of type T, i.e. T or any subclass.

The language rules are written based on this. Thus, if you say

(T2)expression

where the expression has type T, the above sentence means that the compiler can only assume that at runtime, the expression can have type T or some subclass. The cast is legal if there's any possibility that the expression could be cast to T2. In your case, since some of the subclasses of Parent could be cast to Child, then it's legal.

The case of new Parent() might be a special case; now we know that the expression must have type Parent and cannot be of a subclass type. But for Java to make this illegal, the rules would have to be rewritten (language specifications depend on having rules spelled out precisely, so that programs are portable between compilers; compilers aren't really allowed to use "common sense"). The JLS would have to invent the concept of an expression with an "exact type", or something like that, and then a lot of other rules would need to be changed to deal with this sort of expression. A lot of work would be required to add this kind of rule in several places and verify that everything in the language rules is still consistent; and all that work would only affect pathological cases such as your original post that never occur in practice. So it definitely wouldn't be worth it for Java to make this illegal.

Upvotes: 2

Vikas
Vikas

Reputation: 518

It will not throw classcastexception if up-casting of the object is done first then down-casting.

Parent p1=new Child();    
Child chd = (Child) p1; // it will not throw classcastexception

Upvotes: -1

Ravi
Ravi

Reputation: 31407

For simplicity, please see below snippet

 Object x = new Integer(0);
 System.out.println((String)x);

Now, println() method expects String so we type cast x which is type of Object to String. But, at the runtime Object x get instance of Integer. Since, Integer can not be casted to String. It will throw ClassCastException exception at runtime.

Now, coming back to your question, it is an example of down casting. You can not store parent instance to child. Please refer link commented by @Axel in your post.

Upvotes: 0

Related Questions