The meaning of Super() in Java

The meaning of super() in Java subclass constructor

What keeps me wondering is that subclasses are forced to add super() (or super(arg1,arg2,...)) to the first line of the constructor.

In my original view, the subclass and the parent class are inherited but independent of each other. I create a new subclass, why do I need to initialize the parameters of the parent class? Is the parent class to which the parameters of these parent classes that are initialized belong to a concrete object? If the parent object does not exist, what is the point of initializing the parent parameter? Isn't super() unnecessary?

The answer is of course no. The great Java that has existed for so long does not have such low-level problems.

the fact is…

In fact, when a subclass is created, a series of parent classes it inherits will be initialized together. Without loss of generality, we created the Father and Son classes here. Among them, the Son class inherits the Father class.

/**
 * @ author: X3vvv
 * @ date: 13/11/2020
 */
class Son extends Father {
    
    
	// 私有变量hobby
	private String hobby;
	// 构造器
    public Son(String name, String hobby) {
    
    
    	super(name);
    	this.hobby = hobby;
    }
}
public class Father {
    
    
	// 私有变量name
    private String name;
    // 构造器
    public Father(String name) {
    
     this.name = name; }
   
	// 用于测试的主函数
	public static void main(String[] args) {
    
    } // 暂时为空
}

At this time, if we instantiate Son, create a Son object:

	// 用于测试的主函数
	public static void main(String[] args) {
    
    
		Son monkey = new Son("WuKong");
	}

Then this object named monkey has not only the hobby variable defined in the Son class, but everything in the parent class is also created and assigned monkey. It's like the Transformers veteran passed the heavy weapons on his body to Optimus Prime, even the encapsulated parts of the weapons (as private variables) were also passed to Optimus Prime.

Insert picture description here
If necessary, monkey can also use the private variable name in the parent class . Let us take an example.

Example: Use the private variables of the parent class in the subclass

We add two methods speak() and getName() to Father class , and *speak()* method to Son class:

/**
 * @ author: X3vvv
 * @ date: 13/11/2020
 */
class Son extends Father {
    
    
	// 私有变量hobby
	private String hobby;
	// 构造器
    public Son(String name, String hobby) {
    
    
    	super(name);
    	this.hobby = hobby;
    }
    // speak()方法
    public void speak() {
    
    
    	// 调用了从父类继承的函数,以使用属于父类的私有变量name
        System.out.println("^&*#$*.." + this.getName() + "..$%*&");
    }
}

public class Father {
    
    
	// 私有变量name
    private String name;
    // 构造器
    public Father(String name) {
    
     this.name = name; }
    // getName()方法
    protected String getName() {
    
     return name; }
    // speak()方法
    public void speak() {
    
    
        System.out.println("You can call me father, or " + name);
    }
    
	// 用于测试的主函数
	public static void main(String[] args) {
    
    
        Father human = new Father("Xavier");
        Son monkey = new Son("Jack", ""); // hobby只是为了方便说明,故为空值
        human.speak(); 		// 输出结果:"You can call me father, or Xavier"
        monkey.speak(); 	// 输出结果:"^&*#$*..Jack..$%*&"
    }
}

Through testing, it can be found that although only the parent class has the variable name , and because name is a private variable , the subclass cannot use it inside the class. However, by using super() in the Son constructor , initialize the name in the parent class . Then use the public method in the parent class - getName() , and finally the monkey successfully uses the private variable name in the parent class !
Therefore, thanks to the super() in the constructor , even the private variables in the parent class can be used by the subclass.

significance

The level is limited, only two are listed here, and everyone is welcome to add.

  1. Encapsulate variables to prevent arbitrary modification of variables (extended information: the meaning of encapsulation )
  2. Take advantage of the benefits of inheritance-improve the reusability of the code, make the program concise and efficient

(Reference book for this article: "Head First Java")

Guess you like

Origin blog.csdn.net/weixin_39591031/article/details/109674967