Java-1.6 Polymorphism

Source: Internet
Author: User

Java-1.6 Polymorphism

In this section, we will briefly discuss some polymorphism, the last of the three major java features.

1. Concept

Polymorphism literally means "multiple States ". In object-oriented language, multiple implementation methods of interfaces are polymorphism. Reference Charlie Calverts's description of polymorphism-polymorphism is a technology that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features assigned to its sub-objects (from "Delphi4 Programming Technology insider ").

To put it bluntly, the parent class references the subclass object.

(1) Multiple States of different types used for calling

Next we introduce an interface class Person

 

package com.ray.ch01;public abstract class Person {public abstract void eat();public abstract void sleep();}


 

 

The following Boxer and Singer inherit the parent class "Person.


package com.ray.ch01;public class Boxer extend Person {public void fight() {}@Overridepublic void eat() {System.out.println(boxer is eatting);}@Overridepublic void sleep() {System.out.println(boxer is sleeping);}}

package com.ray.ch01;public class Singer extend Person {public void sing() {}@Overridepublic void eat() {System.out.println(singer is eatting);}@Overridepublic void sleep() {System.out.println(singer is sleeping);}}

 

Test:
package com.ray.ch01;public class Test {public static void main(String[] args) {Person boxer = new Boxer();boxer.eat();boxer.sleep();Person singer = new Singer();singer.eat();singer.sleep();}}

Test output:

Boxer is eatting
Boxer is sleeping
Singer is eatting
Singer is sleeping

From the code above, we can see that boxer and singer objects are both Person classes. They do not know the actual needs to call the eat and sleep methods of that version, but why is the final result what we need? The following is a conclusion.

(2) parameters use multiple States of different types

We continue to use the Person, Boxer, and Singer classes, but let's modify the Test class

 

package com.ray.ch01;public class Test {private void test(Person person) {person.eat();person.sleep();}public static void main(String[] args) {Test test = new Test();Person boxer = new Boxer();test.test(boxer);Person singer = new Singer();test.test(singer);}}

In the above Code, we pass the Person parameter to the test, and then the person calls their respective methods.

 

The same problem is that before the parameter is passed in, the person simply doesn't know which version to point.

Test output:

Boxer is eatting
Boxer is sleeping
Singer is eatting
Singer is sleeping

 

(3) the return value uses polymorphism.

We use the Person, Boxer, and Singer classes again, and then modify the Test class.

 

package com.ray.ch01;public class Test {private Person test(Person person) {return person;}public static void main(String[] args) {Test test = new Test();Person boxer = new Boxer();test.test(boxer).eat();test.test(boxer).sleep();Person singer = new Singer();test.test(singer).eat();test.test(singer).sleep();}}

Test output:

 

Boxer is eatting
Boxer is sleeping
Singer is eatting
Singer is sleeping

 

For demonstration, I wrap the code of the test class, return the Person class through a method, and then call their respective methods.

 

So how does java do it? Here I will give a brief description, and then we will detail the chapters below.

In fact, both eat and sleep are generalized methods. They all face the question of which version to call.

For general non-oop languages, they will use the pre-binding method, that is, the memory address of the method already bound at the beginning.

However, java uses post-binding (runtime binding). It uses a special code to calculate the memory address of the execution method. Of course, this code is invisible in the compiler, bind the calculated memory address during running.

 

 

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.