---polymorphism of Java---

Source: Internet
Author: User

Polymorphic:

Can be understood as the existence of a variety of forms of expression.

What is the shape of a man, a woman.

Polymorphism is a subclass of objects that are doing a change.

Cat x = new Cat ();

Animal x = new Cat ();


This is an object that has many forms. X is the cat type, and it is also the "animal" type.

Type promotion: Promote the cat to animal type. Upward transformation


1. Polymorphism of the embodiment

A reference to the parent class points to its own subclass object

A reference to a parent class can also receive its own child class object

2. Conditions for polymorphism

Must be a relationship between a class and a class, or inherit, or implement

Presence Overlay

3. Polymorphism Benefits

Improved program Extensibility

4. The drawbacks of polymorphism

Increased extensibility, but only members of the parent class can be accessed using a reference to the parent class


static binding and dynamic binding:

Static binding: The method that is called can be determined at compile time. Only final, static, private, and constructed methods in Java are static bindings.

Dynamic binding: The runtime is not able to determine which method is being called.

Overloads and Overrides:

Method overloading: Used in a class, the so-called overload is the same method name, but the return value and the parameter list can be different. When the program is running, the compiler automatically chooses a method that is appropriate for this type of parameter, based on the parameters passed in.

Method overrides: Used in inheritance. The methods of the parent class appear in the subclass and are identical (including the return value and the parameter list).

Abstract class animal{    abstract void Eat ();} Class Cat extends animal{public    void Eat ()    {        System.out.println ("Cat.eat ()");}    } Class Dog extends animal{public    void Eat ()    {        System.out.println ("Dog.eat ()");}    } public class practice{public    static void Main (string[] args)    {        cat cat = new Cat ();        Dog dog = new Dog ();        Cat.eat ();        Dog.eat ();        Animal C = new Cat ();//Type promotion  upward transformation        c.eat ();        Animal d = new Dog ();        D.eat ();    }}


Animal C = new Cat (); C.eat (); C.catchmoushe ();


Compilation failed: The method Catchmoushe () is undefined for the type Animal

When you convert a subclass up to a parent class type, the object can only use methods that are owned by the parent class.

The method that is called by the runtime is the subclass object itself.

How do I implement cat-specific methods?

Downward transformation.

Cat A = (cat) C;

Determine if a reference of a certain type is of this type: instanceof

if (c instanceof cat) {    ((cat) c). Catchmoushe ();}


Features of non-static member functions in polymorphic states:

Compile time: See if there is a method called in the class to which the referenced variable belongs

If there is a compile pass, there is no failure

Run time: See if there are methods called in the class to which the object belongs

Non-static member function in polymorphic invocation, compile look left, run look right

That is to say: In the subclass, the method that calls the subclass, does not go to the parent class to find. If not, it is not compiled.

The characteristics of member variables in polymorphism: Compile and run all refer to left.

class fu{    int num = 10;} Class Zi extends fu{    int num = 20;} Class main{public    static void Main (string[] args)    {        Fu f = new Zi ();        Zi z = new Zi ();        System.out.println (f.num+ "\ n" +z.num);}    }


The result of the output is:

10

20

Features of static member functions: refer to left.

class fu{    int num = ten;    static void Show ()    {        System.out.println ("Fu.show ()");}    } Class Zi extends fu{    int num =;    static void Show ()    {        System.out.println ("Zi.show ()");}    } Class main{public    static void Main (string[] args)    {        Fu f = new Zi ();        Zi z = new Zi ();        F.show ();        Z.show ();    }}


Output Result:

Fu.show ()

Zi.show ()





See an example in someone else's article:



Class A {public String show (D obj) {return ("A and D");           Public String Show (a obj) {return ("A and a");           }} class B extends a{public String show (b obj) {return ("B and B");           Public String Show (A obj) {return ("B and A"); }} class C extends b{} class D extends B{}public class Duotai {public static void main (string[] args) {A a1 = new A (          );          A A2 = new B ();          b b = new B ();           c C = new C ();           D d = new D ();  System.out.println (A1.show (b));  ①system.out.println (A1.show (c));  ②system.out.println (A1.show (d));  ③system.out.println (A2.show (b));  ④system.out.println (A2.show (c));  ⑤system.out.println (A2.show (d));   ⑥system.out.println (B.show (b));   ⑦system.out.println (B.show (c)); ⑧system.out.prIntln (B.show (d)); ⑨}}


Output Result:



/*a and a   ①a and a   ②a and D   ③b and a   ④b and a   ⑤a and D   ⑥b and B   ⑦b and B   ⑧a and D   ⑨ */


This involves the priority of the method invocation.

The priority is from high to low in order:

This.show (O),

Super.show (O),

This.show (Super) O),

Super.show ((Super) O).

Give an example:

①:a1.show (b);

A1 is an object of Class A, first the method called by this.show (o) first goes to Class A, the arguments passed are Class B objects, there is no such method in a, then press super.show (o)

To find, find a parent class, a no parent class, and then execute this.show (super) O to find the Class B's parent class, Class B is the parent class A, in Class A has the method, output.

②:a1.show (c)

Or the first to find a method, a does not, look for A's parent class, a no parent class, look for C's parent class, C's parent class is B, a or there is no method, then find the parent of a b,a no parent class

Looking for the parent of B, the parent class of B is the method in A,a, output.





---polymorphism of Java---

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.