Reference and excerpt from: http://www.runoob.com/java/java-polymorphism.html
Polymorphic
Polymorphism is the ability of a behavior to have many different forms of expression.
Polymorphism is the same interface that uses different instances to perform different operations.
Three prerequisites for polymorphic presence:
1. Inheritance 2. Rewrite 3. Parent class reference to child class object
such as parent a = new Child ();
When calling a method using polymorphic mode, first check whether the method is in the parent class, if not, compile the error, and if so, call the subclass's same name method.
The benefits of polymorphism: The program can be well extended, and the objects of all classes can be universally handled.
Polymorphic instances:
Abstract classanimal{Abstract voideat ();}classCatextendsanimal{ Public voideat () {System.out.println ("Eat fish"); } Public voidWork () {System.out.println ("Catch Mouse"); }}classDogextendsanimal{ Public voideat () {System.out.println ("Eat Bone"); } Public voidWork () {System.out.println ("Watch Home"); }} Public classtest{ Public Static voidMain (string[] args) {Show (NewCat ()); Call the Show method show with a Cat object (NewDog ()); Call the Show method with the dog object Animal a=NewCat ();//Upward transformation a.eat (); Cat C=(Cat) A;//downward Transformation c.work (); } Public Static voidShow (Animal a) {a.eat ();
Type Judgmentif(AinstanceofCat) {Cat C=(Cat) A; C.work (); }Else if(AinstanceofDog) {Dog C=(Dog) A; C.work (); } }}
// Run Results Eat fish Catch mouseeat bonewatch homeeat Fish Catch Mouse
Virtual method:
classanimal{ PublicAnimal (inta) {System.out.println ("Superclass ..."); } Public voideat () {System.out.println ("Superclass test ...."); } Public voidEat2 () {System.out.println ("Superclass eat2 ..."); } Public voiddoeat () {//empty method, actually running the subclass method }}classCatextendsanimal{ PublicCat () {Super(100);//when the parent class constructor takes parameters, the child class constructor must call the parent class constructor with a parameter by super. System.out.println ("Subclass ..."); } Public voidEat2 () {System.out.println ("Subclass Eat2 ..."); } Public voiddoeat () {eat ();//parent class has, subclass no, subclass call directlyEAT2 ();//parent class has, subclass overrides, call subclass method call directly This. EAT2 ();//Ibid . Super. EAT2 ();//parent class has, subclass overrides, call parent class method with Super } } Public classtest{ Public Static voidMain (string[] args) {Animal a=NewCat ();//Parent class reference to child class objecta.doeat (); }}
Operation Result:
Superclass ...
Subclass. Superclass Test....subclass Eat2...subclass eat2...superclass eat2 ...
At compile time, the compiler uses the method validation in the parent class, which executes using a subclass method called a virtual method call, which is called a virtual method.
All methods in Java can behave in this way, so the overridden method can be called at run time, regardless of the data type of the reference variable in the source code at compile time.
How to implement a polymorphic pattern:
Way one: Rewrite
Mode two: interface
Way three: Abstract classes and abstract methods
Java Tour _ Object-oriented _ polymorphism