Three characteristics of Java-----------------------polymorphism

Source: Internet
Author: User

The polymorphism of three major features of Java


Polymorphic definition: means that objects of different classes are allowed to respond to the same message, that is, the same message can be used in various ways depending on the sending object.

As far as I am concerned, it is still a bit difficult for a new person to understand the definition at the outset (perhaps I am a bit stupid). I think the Java way to understand it might be a little simpler:

Polymorphism refers to the specific type of reference variable defined in the program and the method call issued by the reference variable is not determined during programming, but is determined during the program's run, that is, a reference to which the variable bottom will point to which class the instance object, the reference variable emitted by the method call is exactly the method implemented in the class. Must be determined by the time the program is running. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism.

To give a familiar example, "Mulan poem," There is a "double rabbit to walk, Ann can tell me that I am male and female"; the females (A) and the male rabbits (B) are all children of rabbits (rabbit), but when you are away from the two rabbits, you can only pick up the eyes to see the difference (the male rabbit's feet flutter, the female rabbit eyes blurred). Below we can use Java code to represent "Double rabbit":

Rabbit a = female rabbits;

Rabbit b= male rabbit;

Here is polymorphic, the female and male rabbits are the children of rabbits, we can refer to different subclasses through the parent class-------we only run until the reference type points to the specific instance object

A a= new A ();

This is easy to understand, just to instantiate a female rabbit object, but you see the following???

Rabbit r= new A ();

We can understand that this defines a rabbit R, which points to the A object instance. Because A and rabbit are inheritance relationships, a can be transformed upward into rabbit, so r is an instance object that can point to a ... This has a very, very good benefit: in the last inheritance we said that the subclass is the extension of the parent class, it can provide a more powerful function than the parent class, and if we define a reference type that refers to the parent class of the subclass, he can also use the subclass's powerful functionality in addition to referencing the generality of the parent class.

However, there are some flaws in the upward transformation, that is, it inevitably leads to the loss of some of the methods ' attributes, so that we cannot get them: so there is: the parent class type reference can call the methods and properties defined in the parent class, but the methods and properties in the subclass are no more.


Let's take a look at the code:

public class rabbit{

public void Fun1 () {

System.out.println ("------------------->rabbit's Fun1 ()")



public void fun2 () {

System.out.println ("----------Rabbit's Fun2 ()")

}

}

}


public class A () {

/*

* Subclass overloads the method of the parent class, the method does not exist in the parent class, and the parent class cannot reference the method

*/

public void Fun1 (String a) {

System.out.println ("------------->a's Fun1 ()")

}

/*

* Subclasses overriding the parent class method, referring to the subclass's parent class reference when calling Fun2 () will inevitably call *fun2 () in the subclass

*/

public void fun2 () {

System.out.println ("----------->a's Fun2 ()")

}

}

Let's Test it:

public class test{

public static void Main (String args[]) {

Rabbit r = new A ();

R.fun1 ();

R.fun2 ();

}

}

______________________________________________________________________

Output:

The >rabbit of the-------------------------fun1 ()

The >a of the--------------------------fun2 ()



So for polymorphism we can summarize the following:

A parent class reference to a subclass because it has been transformed upward, it can only access methods and properties that are owned in the parent class, and the reference is not available for methods that exist in the subclass and that do not exist in the parent class, although the method is overloaded. If subclasses override some of the methods in the parent class, they must be called using the methods defined in the subclass (dynamic joins, dynamic calls).



Multi-State Classic case:

public class A {
Public String Show (D obj) {
Return ("A and D");
}

Public String Show (A obj) {
Return ("A and a");
}

}

public class B extends a{
Public String Show (B obj) {
Return ("B and B");
}

Public String Show (A obj) {
Return ("B and A");
}
}

public class C extends b{

}

public class D extends b{

}

public class Test {
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 ();



//priority from highest to lowest:
//This.clarify (O), Super.clarify (O), this.clarify (Super) O, Super.clarify (Super)
//A2.clarify (b), A2 is a reference variable, Class A, then this is A2,
//b is an example of B, so it finds clarify (b obj) in Class A, not found,
//Then to a super (superclass) to find, and a no superclass, so go to the third priority this.clarify (Super) O),
//This is still A2, where O is B, (super) O IE (super) b is a, so it is looking for clarify (a obj) in Class A,
//Class A has that trick, but since A2 refers to an object of Class B, B covers A's clarify (a
//obj), so it is eventually locked to the clarify (A obj) of Class B, and the output is "B and A"

System.out.println ("n" + a1.show (b));
System.out.println ("2--" + a1.show (c));
System.out.println ("3--" + a1.show (d));
System.out.println ("4--" + a2.show (b));
System.out.println ("5--" + a2.show (c));
System.out.println ("6--" + a2.show (d));
System.out.println ("7--" + b.show (b));
System.out.println ("8--" + b.show (c));
System.out.println ("9--" + b.show (d));
}
}

Summary:

, polymorphic refers to the specific type of reference variable defined in the program and the method call issued by the reference variable is not determined at compile time, but is determined during the run of the program, that is, a reference variable to which the bottom of the object will point to the instance of the class, the reference variable emitted by the method called in the class is the method of implementation , it must be determined by the time the program is running. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism.

2. The parent class reference to a subclass because it has been transformed upward, it can only access methods and properties that are owned in the parent class, and the reference is not available for methods that exist in the subclass and that do not exist in the parent class, although the method is overloaded. If subclasses override some of the methods in the parent class, they must be called using the methods defined in the subclass (dynamic joins, dynamic calls) .

3. The invocation of an object method in the inheritance chain has a priority: This.show (O), Super.show (O), This.show ((Super) O), Super.show ((Super) O). That is, the parent class of this object is checked first, and the parent class of the parameter is not checked again.


Hope to give you help ~ ~ ~ (>_<) ~ ~ ~!!!!!














































Three characteristics of Java-----------------------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.