The exaception thrown to indicate that your code has attempted to cast an object to a subclass of which it is not an instance. This means that ClassCastException occurs when you try to cast an instance of an Object to a type that it is not. Type Casting 5 only works when the casted object follows an is a relationship to the type you are trying to cast to.
It is good practice to guard any explicit casts with an instanceof check first:
if (myApple instanceof Fruit) {
Fruit myFruit = (Fruit)myApple;
}
When will be ClassCastException is thrown: