What are the characteristics of Java polymorphic member access?

  • 2021-09-16 06:53:56
  • OfStack

Characteristics of Java Polymorphic Member Access

Polymorphic summary

Polymorphism is one of the three characteristics of object-oriented programming. I will not introduce it too much here. I have the opportunity to write an article on inheritance, encapsulation and polymorphism.
I have sorted out 1 part about this aspect in the learning module of force buckle before, so I can have a simple understanding of 1 first.
Force deduction compensation-object-oriented-inheritance

Java polymorphism is most commonly used in scenarios where parent class references point to subclass objects.

The premise of polymorphism:

There is an inheritance relationship (Son inherits Father) There is a method override (Son overrides the Walk method in Fahter) Parent class references point to subclass objects ( Father father = new Son(true); )

Characteristics of Member Access in Java Polymorphic Scenarios

There are three parts of access rules in polymorphic scenario, which are access to member variables, member methods and static methods.
Here only copy the polymorphic test class code, and briefly introduce the Father class and Son class under 1.
There is 1 in the Father class boolean canRun Variable fixed to False Of the subclass canRun Member variables are passed in by constructors.


walk(boolean canRun): If canRun Yes True Output Father/Son Can run , Otherwise output Father/Son Can only walk .

public class Test {
    public static void main(String[] args) {
        Father father = new Son(true);
        System.out.println(father.isCanRun());
        father.walk(father.isCanRun());
        father.run();
    }
}

Member variable access rules

Father father = new Son(true);
The access formula of member variables is to make up the parent to transport the parent or make up the left to transport the left, and simplify it into looking at the parent class in one step.
Explanation: In the compilation stage, as long as the parent class can pass the compilation, no matter how the class on the right is defined, it will not report an error, and the execution result in the execution process is also determined according to the variables in the parent class.
Although we give Son The parameter passed in is true But for members father Access to variables in is still determined by the parent class.
So when we use father.isCanRun() Visit canRun Variable, the result returned is boolean canRun0

Member method access rules

The member method access formula is to make up the father's luck or make up the left luck and right luck.
Expansion interpretation means that as long as the parent class can pass compilation in the compilation stage, no matter how the class on the right is defined, it will not report an error. The execution result in the execution process needs to be determined according to the overridden method in the subclass (the overridden method in the son returns whether the son can run or not).
So when we call father.walk() Method, the result returned is 儿子只能跑! .

Static method access rules

The formula of static method access is to weave parent to weave parent or weave left to weave left, and member variable 1 is to look at parent class.


/*
	 Add static methods to both parent and child classes 
	*/
	//  Parent class 
    public static void run(){
        System.out.println(" Father met the robber ! Have to run !");
    }
    //  Subclass 
    public static void run(){
        System.out.println(" My son met a robber ! Have to run !");
    }

Call father.run() The result returned is 父亲遇到抢劫犯了!不得不跑!
It is proved that when a static method is called in a polymorphic situation, it is still the static method defined in the left-hand-parent class of the call.

My understanding of this part is not particularly profound, and I want to supplement the concentration situation when I have time in the future
Whether the subclass can call the static method of the parent class in the static code block and so on
Continue to learn backward first, and then sort out the relevant knowledge of Java virtual machine. Combined with the loading of classes, we may have a deeper understanding of this part later.

Reference/extended reading

The difference between method overloading and method rewriting of Java basic series No.1 bomb

Java Basic Series 3 of the operation string class have what? What's the difference?


Related articles: