First, the basic concept
polymorphic:
The focus is on the polymorphism of the object. A variety of manifestations of a thing.
The role of polymorphism:
1. Improve the extensibility of the code, the late appearance of the function, can be executed by the previous program.
2. You cannot use features that are specific to subclasses. You can only use features that override the parent class.
Prerequisites for polymorphism:
1. It is necessary to have an inheritance relationship or to implement a relationship.
2. The method is usually overridden and overwritten.
3. Parent class or interface to refer to child class objects.
Multi-state upward transformation:
1. Improve the extensibility of the program, hide the specific subtype
2. You can only use features in the parent class, and you cannot use features that are specific to subclasses. The function is limited.
Multi-state downward transformation:
1. You can use the unique features of subtypes.
2. The specific subtype must be faced.
3. In order to avoid classcastexception problems at runtime, it is necessary to use the keyword instanceof to judge.
Second, the code example
1 //Parent Class Animal2 Abstract classAnimal3 {4 Abstract voideat ();5 Abstract voidsleep ();6 }7 8 //Dog class inherits animal class9 classDogextendsAnimalTen { One /** A to override a method in the animal class - */ - voidEat () the { -System.out.println ("Dog Eat Eats Bones"); - } - voidsleep () + { -System.out.println ("Dog sleep ..."); + } A at /** - your own unique approach. - */ - voidSay () - { -System.out.println ("Dog say ..."); in } - } to + //Cat class Inherits animal class - classCatextendsAnimal the { * /** $ to override a method in the animal classPanax Notoginseng */ - voidEat () the { +System.out.println ("Cat Eat eats fish"); A } the voidsleep () + { -System.out.println ("Cat sleep ..."); $ } $ } - - classDuotaidemo the { - //methods for encapsulating the dog classWuyi Public Static voidmethod (Dog D) the { - d.eat (); Wu d.sleep (); - D.say (); About } $ - //methods for encapsulating cat classes - Public Static voidmethod (Cat C) - { A c.eat (); + c.sleep (); the } - $ //Methods for Encapsulating parent class animal the Public Static voidmethod (Animal s) the { the s.eat (); the s.sleep (); - } in the Public Static voidMain (string[] args) the { AboutDog D1 =NewDog (); the method (D1); the theCat C =NewCat (); + method (c); - theAnimal s =NewDog ();//Polymorphism , upward transformation, the dog is promoted to animal. BayiMethod (s);//only the Eat method and the sleep method that cover the parent class animal can be used. You cannot use your own say method in the dog class. the theAnimal A =NewCat ();//Polymorphic , upward transformation. - method (a); - the /** the to avoid classcastexception problems at run time , the you need to use keyword instanceof to make judgments. the */ - if(sinstanceofDog) the { theDog D2 =(Dog) s; the d2.eat ();94 d2.sleep (); the D2.say (); the } the } 98}
Third, run the code
Java Object-oriented polymorphic (up-down transition) Getting started instance