JAVA Difficult Problem Analysis----Parent and Child Class Attribute Duplicate Name Problem in Inheritance System

Subclass method inheritance essence, whether subclass inherits parent class private property

  • In JAVA, if there are static variables in the inherited parent class, multiple subclasses still operate on the same static variable address, so the operations of the subclasses must overwrite each other.
    If the subclass overrides the methods and static variables of the parent class, it is exclusive to the subclass, and the operations of other subclasses will not overwrite each other. At this time, the static variables inherited from the parent class will be hidden and can be accessed through the class name of the parent class.

. The difference between rewriting and overloading: both are characteristics of methods

Overloaded Implementation Conditions :
    multiple methods in a class
    same method name
    The parameter list is different|method signature is different
 overridden fulfillment condition:
    two classes
    There is inheritance between classes|achieve relationship
    The method signatures (method name and parameter list) are identical

1. The subclass inherits the method of the parent class but does not override it

public class Test05{
	public static void main(String[] args) {
		Father f = new Father();
		Son s = new Son();
		System.out.println(f.getInfo());
		System.out.println(s.getInfo());
		s.test();
		System.out.println("-----------------");
		s.setInfo("Greater Silicon Valley");
		System.out.println(f.getInfo());
		System.out.println(s.getInfo());
		s.test();
	}
}
class Father{
	private String info = "atguigu";
	public void setInfo(String info){
		this.info = info;
	}
	public String getInfo(){
		return info;
	}
}
class Son extends Father{
	private String info = "Silicon Valley";
	public void test(){
		System.out.println(this.getInfo());
		System.out.println(super.getInfo());
	}
}

The member attribute of the subclass and parent class has the same name, but when calling the method, it is found that the subclass does not override the method of the parent class, so when calling the method of the parent class, according to the principle of proximity, calling the attribute with the same name uses the attribute with the closest distance by default. When a subclass calls a method inherited from a parent type, the data that is manipulated is the data in the characteristics of the parent type.

2. The pointer of this pointer is unique

package com.atguigu.day13;
public class Test01 {
    public static void main(String[] args) {
        Father f = new Son();
        System.out.println(f.x);
    }
}
class Father{
    int x = 10;
    public Father(){
        this.print();
        x = 20;
    }
    public void print(){
        System.out.println("Father.x = " + x);
    }
}
class Son extends Father{
    int x = 30;
    public Son(){
        this.print();
        x = 40;
    }
    public void print(){
        System.out.println("Son.x = " + x);
    }
}

Although the this pointer is used in the parent class member here, the this pointer only points to the calling object, that is, the subclass.

3. Call selection of virtual method

Polymorphic member access:
Whether the compilation is passed: whether the parent class has this member
Member variable: the execution is the parent class variable (no polymorphism)
Member method: There is rewriting to execute the subclass, and there is no rewriting to execute the parent class
virtual method
(1) Static dispatch: first look at the compile-time type of this object xx, and find a matching method in the compile-time type of this object
The principle of matching: look at the degree of match between the compile-time type of the actual parameter and the type of the method parameter
A: Find the best match Compile-time type of actual parameter = type of method parameter
B: Find compatible compile-time type of actual parameter < type of method parameter
(2) Dynamic binding: Look at the runtime type of this object xx again. If the runtime class of this object xx overrides the matching method just found, then execute the rewritten method, otherwise it still executes the type in the compile-time type just now The method that matches

public class Test09 {
	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();
		System.out.println("(1)" + a1.show(b));
		System.out.println("(2)" + a2.show(d));
		System.out.println("(3)" + b.show(c));
		System.out.println("(4)" + b.show(d));
	}
}
class A {
	public String show(C obj) {
		return ("A and C");
	}
	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 {

}

4. Polymorphic references

public class Test10 {
	public static void main(String[] args) {
		Base b = new Sub();
		System.out.println(b.x);
	}
}
class Base{
	int x = 1;
}
class Sub extends Base{
	int x = 2;
}

Polymorphic reference refers to the polymorphism of the method. There is no polymorphism in member variables, only the compiled type

Tags: Java jvm programming language

Posted by shantred on Wed, 29 Mar 2023 06:08:07 +1030