I have a class C. Class E extends it.
E e = new E();
C c = new C();
Why is
e = (E) c;
Upon further review: though numeric conversions have the same syntax as casting objects, some confusion arose. At any event, the above does not give a compilation, but rather a runtime error – so a class can be casted to subclass in some instances (otherwise the code would not compile). Any examples that anyone can give where the above works?
And also:
K extends M
K k = new K();
((M) k).getClass() gives K. Why is that? It was casted to the more general M!
Suppose I have a doIt() method implemented in both M and K. executing
((M) k).doIt();
gives M’s or K’s doIt()?
Thanks!
Consider a real-world example:
All dogs are animals, but not all animals are dogs. Hence…
Casting an Animal to a Dog can only be done if the Animal in question is indeed a Dog. Otherwise it would force the Universe to infer properties unique to a dog (wagging tail, barking, etc.) onto an Animal. That Animal might well be a Cat with properties unique to it (purring, rigorous regime of self-cleaning, etc.). If the cast is not possible then a ClassCastException is thrown at runtime.
Nobody wants a dog that purrs.
You’ve casted k to M, but all classes have a getClass() method. k’s class is always K, regardless of whather you cast its reference to M or not. If you cast a Dog to an Animal and ask it what animal it is it’ll still answer that it’s a dog.
In fact, casting to a superclass is redundant. A Dog already is an Animal and it has all the methods of an Animal as well as its own. Many Code Analysis tools such as FindBugs will notify you of redundant casts so you can remove them.
K’s doIt() for the same reasons as above. The cast operates on the reference; it doesn’t transform an object to a different type.
Sure can. Imagine a method that receives a list of animals for processing. All the dogs need to be taken for a walk, and all the cats need to be played with using a bird-shaped toy. To do this we call the
takeForWalk()method that only exists on Dog, or theplay()method which only exists on Cat.