Eg2 : Public class Test { Public Static void Main (string[] args) { Show (new Cat ()); Call the Show method with a Cat object Show (new Dog ()); Call the Show method with a Dog object Animal a = new Cat (); Upward transformation A.eat (); The eat of the Cat is called Cat C = (cat) A; Down transformation C.work (); The catchmouse of the Cat is called } Public Static void Show (Animal a) { A.eat (); Type judgment if (A instanceof Cat) {//cat do things Cat C = (cat) A; C.work (); } Else if (a instanceof dog) {//dog do things Dog C = (dog) A; C.work (); } } } Abstract class Animal { Abstract void eat (); } class Cat extends Animal { Public void eat () { System. out. println ("Eat fish"); } Public void work () { System. out. println ("Catch Mouse"); } } class Dog extends Animal { Public void eat () { System. out. println ("Eat Bones"); } Public void work () { System. out. println ("housekeeping"); } } |