Deep understanding of Java polymorphism

Source: Internet
Author: User

Object-oriented programming has three main characteristics: encapsulation, inheritance, polymorphism.

Encapsulation hides the internal implementation mechanism of the class, and can change the internal structure of the class without affecting the use, while protecting the data. To the outside, its internal details are hidden, and what is exposed to the outside world is the way it is accessed.

Inheritance is to reuse the parent class code. Two classes can use inheritance if they have a is-a relationship. , at the same time, inheritance also for the implementation of the foreshadowing of polymorphism. So what is polymorphism? What is the implementation mechanism of polymorphism.

Polymorphism means that the specific type of reference variable defined in the program and the method call issued through the reference variable are not defined programmatically. It is only when the program is running that a reference variable will point to the instance object of which class, and the method called by the reference variable is the method implemented in which class. Must be determined by the time the program is run. Because the specific class is determined when the program is run, in this way, without modifying the source code, the reference variable can be bound to a variety of different class implementations, which causes the specific method of the reference call to change, that is, without modifying the program code can change the code that the program is bound to, so that the program can select multiple running states, This is polymorphism.

Instance code: (Polymorphism based on inheritance implementation)

Package com.nocol.extend;

/** * @author LXP * * * * * * @TODO * * * * * * *
/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 Test_duotai {public
	static void main (St Ring[] args {
		A a1 = new A ();
		A A2 = new B ();
		b b = new B ();
		c C = new C ();
		D d = new D ();

		System.out.println ("1--" + 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)); 
	}
Run Result:


Look at the results here 1, 2, 3 good understanding, starting from 4 began to be confused, for 4 why the output is not "B" and B.

First, let's look at a sentence: When a superclass object references a variable referencing a subclass object, the type of the referenced object, rather than the type of the reference variable, determines whose member method to invoke, but the invoked method must be defined in the superclass, that is, the method overridden by the quilt class. This sentence is a generalization of polymorphism. In fact, 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)

Analysis:

From the above program we can see that a, B, C, D are the following relationships.

First we analyze 5,a2.show (c), A2 is a reference variable of type A, so this represents A,a2.show (c), which is found not found in Class A, and is found in the superclass of a (super), because A has no superclass (except object), so skip to the third level, That is, This.show ((Super) O), C has a super Class B, A, so (super) O is B, A,this is also a, here in a find show (a obj), and since A2 is a reference to Class B and Class B overrides show (a obj), Therefore, the show (a obj) method of subclass B is eventually called, and the result is B and a.

I can also confirm other answers in the same way.

Method has been found, but we still have a little doubt here, let's just say this: When a superclass object refers to a variable referencing a subclass object, the type of the referenced object, not the type of the reference variable, determines whose member method to invoke, but the called method must be defined in the superclass. That is to say, the quilt class covers the method. Here we use an example to illustrate what this sentence means: A2.show (b);

Here A2 is a reference variable, is a type A, it refers to a B object, so according to the above sentence means that there is B to determine who to call the method, so A2.show (b) should be called Show (b obj) in B, the resulting result should be "B and B", But why is it different from the previous running results? Here we ignore the following sentence "but the method called here must be defined in the superclass", so show (B obj) exists in Class A. It doesn't exist at all. So this sentence does not apply here. So this is the wrong sentence. Not too. In fact, this sentence also implied that this sentence: it is still in the inheritance chain to call the priority of the method to confirm. So it finds show (a obj) in Class A and because B overrides the method, it calls the method in class B, or it calls the method in Class A.

So the principle of polymorphic mechanism is summarized as follows: When a superclass object references a variable referencing a subclass object, the type of the referenced object, rather than the type of the reference variable, determines whose member method to invoke, but the invoked method must be defined in the superclass, that is, the quilt-covered method, However, it still has to confirm the method based on the precedence of the method call in the inheritance chain: This.show (O), Super.show (O), This.show (Super) O, Super.show (Super) O).




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.