Phase 1 Language Basics + downcast senior _1-3-Java language senior _02- inheritance and polymorphism _ _21_ Section 4 of polymorphic objects


Upcast drawbacks
here to define your own method catchMouse in the subclass which

go to call here a unique subclass method can not be invoked to.

Cats can catch mice, but not all sub-classes have methods to catch mice

Downcast reduction

Has always been to become a cat a cat can restore





code demonstrates the


need to cast it



back casts, it is a subclass method can be called.

Create a dog also inherited class, Animal class. The method of rewriting eat inside the



abnormal

class cast exception








 

Guess you like

Origin www.cnblogs.com/wangjunwei/p/11221025.html