Java Tour _ Object-oriented _ polymorphism

Source: Internet
Author: User

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 Judgment
if(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.