Question about Java polymorphism and casting

后端 未结 9 2307
谎友^
谎友^ 2020-12-29 12:02

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

9条回答
  •  粉色の甜心
    2020-12-29 12:13

    Consider a real-world example:

    public class Dog extends Animal
    

    All dogs are animals, but not all animals are dogs. Hence...

    public class Cat extends Animal
    

    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.


    ((M) k).getClass() gives K. Why is that? It was casted to the more general M!

    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.


    Suppose I have a doIt() method implemented in both M and K. executing

    ((M) k).doIt();

    gives M's or K's doIt()?

    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.


    Can you give an example of when casting (Dog doggy = (Dog) myAnimal) makes sense?

    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 the play() method which only exists on Cat.

    public void amuseAnimals( List animals ) {
        for ( Animal animal : animals ) {
             if ( animal instanceof Dog ) {
                 Dog doggy = (Dog)animal;
                 doggy.takeForWalk( new WalkingRoute() );
             } else if ( animal instanceof Cat ) {
                 Cat puss = (Cat)animal;
                 puss.play( new BirdShapedToy() );
             }
         }
    }
    

提交回复
热议问题