preface

  • inJavaAs one of the three characteristicspolymorphismIs important, especially in the form of method overloading (Overload), method rewriting (Override)
  • This article is comprehensive & detailed analysisJavaI hope you’ll enjoy the polymorphic properties of

directory


Definition 1.

For different instance objects of the same type, the same behavior has different manifestations


2. The role

A. Eliminate coupling between the same types; b. Enable different objects to have multiple representations of the same behavior

For example, for the “dinosaur” class, the specific object can be “flying dragon” and “tyrannosaurus”, for the same “roar” behavior, the roar sound is different.


Principle 3.

3.1 Knowledge base: Static and dynamic types of variables

  • Static type of a variable = reference type = compile-time variable: never changed, known to the compiler
  • Dynamic type of variable = instance object type = runtime variable: changes, known at run time

See the following example:

Public class Test {// Human extends Human {} public class Test {// Human extends Human {} public class Test {// Human extends Human {} static Class Woman extends Human {} public static void main(String[] args) {Human = new man (); // Static type of variable man = reference type = compile-time variable = Human: will not change, in the compiler known // dynamic type of variable man = instance object type = runtime variable = man: will change, in the runtime known}}Copy the code

3.2 Implementation Principles

By assigning a subclass object instance to a parent class reference variable, static variables at compile time are different from dynamic variables at run time, and polymorphism occurs when a method/variable is called.

3.3 Application Examples

B = new B(); // compile time variable = A b, run time variable = new b () b.name; // call the parent class A member variable name b.move(); Move (), content() b.tent (); move () b.tent (); // Conclusion: Polymorphisms occur because the subclass object reference is assigned to the superclass object variable, i.e. A A = new B(), i.e. the compile-time variable is not the same as the run-time variableCopy the code

4 Implementation Process (direct pointer access)



  1. JVMThe VM uses the reference type (reference, that is, A reference to) queryJavaThe local variable table in the stack

Gets the address of the object type data in the heap

  • Based on the address to find the object type data in the method area (BObject type data)
  • The method table in the query object type data locates to the actual class (BClass) to run
  • Note: Basic knowledge supplement

    • To fully understand the implementation of polymorphism, the following is also necessaryJVMBasic knowledge of
    • For:A a = new B()

    A. Data storage mode

    B. Implementation of reference type access

    • Q: Once an object is created, how do I access it?

      What you really need to access is object instance data & object type data

    • A: Java programs access objects on the Java heap through reference data on the stack

    In the Java virtual machine, reference only specifies a reference to an object, but it does not define how the reference should locate and access the specific location of the object in the heap

    So object access depends on the virtual machine implementation. At present, there are two mainstream object access methods:

    • Handle access
    • Direct pointer access

    Please see the following introduction for details:


    5. Type

    • Mainly includes: compile-time polymorphism & runtime polymorphism
    • The implementations are: method overloading (Overload, pre-binding), method override (Override, post-binding)

    In the next section, I will go into method Overload & method Override in detail.


    6. Method Overload

    6.1 introduction

    6.2 Specific Usage

    Public class Test {public class extends Human {} public class extends Human {} public class extends Human {} public class extends Human { Class Woman extends Human {} class Woman extends Human {} Public void sayHello(Human guy) {system.out.println (" Hello,guy!") ); } public void sayHello(Man guy) { System.out.println("hello gentleman!" ); } public void sayHello(Woman guy) { System.out.println("hello lady!" ); } public static void main(String[] args) {Human man = new man (); Man woman = new Woman(); Test test = new Test(); test.sayHello(man); test.sayHello(woman); }} // Run the result hello,guy! hello gentleman!Copy the code

    6.3 Principle: Static Dispatch

    • Defines the behavior of method dispatch based on the static type of a variable

      1. That is, determining which method to execute based on the static type of the variable
      2. It happens at compile time, so it doesn’tJavaVirtual machine to execute
    • The principle of analytic

    Public class Test {public class extends Human {} public class extends Human {} public class extends Human {} public class extends Human { Class Woman extends Human {} class Woman extends Human {} Public void sayHello(Human guy) {system.out.println (" Hello,guy!") ); } public void sayHello(Man guy) { System.out.println("hello gentleman!" ); } public void sayHello(Woman guy) { System.out.println("hello lady!" ); } public static void main(String[] args) {Human man = new man (); Man woman = new Woman(); Test test = new Test(); test.sayHello(man); test.sayHello(woman); }} // Run the result hello,guy! hello gentleman! A. Method OverLoad = static assignment = which method to execute based on the static type of the variable // b. SayHello (Human guy, man guy, man guy, man guy, man guy, man guy, man guy); SayHello (Human guy), sayHello(Man guy)Copy the code

    7. Override method

    7.1 introduction

    7.2 Usage

    Public void sayHello(){system.out.println ("Human say hello"); SayHello () class Man extends Human {@override protected void sayHello() {system.out.println (" Man ") say hello"); } } class Woman extends Human { @Override protected void sayHello() { System.out.println("woman say hello"); Public static void main(String[] args) {1 Human man = new man(); man.sayHello(); // situation 2 man = new Woman(); man.sayHello(); SayHello (); sayHello(); sayHello();Copy the code

    7.3 Principle: Dynamic Dispatch

    • Defines the behavior of method dispatch based on the dynamic type of a variable

      That is, determining which method to execute based on the dynamic type of the variable

    • The principle of analytic

    Public void sayHello(){system.out.println ("Human say hello"); SayHello () class Man extends Human {@override protected void sayHello() {system.out.println (" Man ") say hello"); } } class Woman extends Human { @Override protected void sayHello() { System.out.println("woman say hello"); Public static void main(String[] args) {1 Human man = new man(); man.sayHello(); // situation 2 man = new Woman(); man.sayHello(); }} man say hello woman say hello Method Override = dynamic dispatch = Determine which method to execute (Override) based on the variable's dynamic type // 2. For case 1, the override sayHello() method in Man is called based on the variable's dynamic type (Man). For case 2: call overriding method sayHello() in woman based on variable (Man) dynamic type (woman)Copy the code

    8. Method Overload vs. method Override comparison

    • conclusion

    • Contrast & difference

    This concludes the presentation of polymorphic features in Java.


    9. To summarize

    • In this article, we will focus on polymorphism, one of the three major features in Java. We will focus on its manifestation: method Overload, method Override.

    • I’m going to continue to talk about Android & Java in more detail. If you’re interested, check out Carson_Ho’s Android development notes


    Please give the top/comment a thumbs up! Because your encouragement is the biggest power that I write!