1. How to use super in Java to call the constructor of the parent class? So why don't we directly inherit from it, but we need to return and call it again?
To call the constructor of the parent class, because the constructor cannot be inherited, you need to call the parent class. There are two situations: 1. When the parent class has a default constructor, the subclass automatically calls the constructor of the parent class. 2. When the parent class has a constructor with parameters, the subclass needs to use the super keyword to call the constructor of the parent class, it must be placed in the first sentence of the subclass constructor.All classes have constructor methods. Even if you do not write them, a constructor with or without parameters is created by default. So there will be a constructor called a () in your. When you are new A (), by default, you have the first sentence in a method with or without parameters constructor A (). A super (); method is automatically added, this is the meaning of calling the parent class constructor, which is a rule stipulated by Java. You can try to write a constructor in a: A () {super (); // This must be placed in the first system. out .. XX;} This is the same effect as if you didn't write super (), because if you didn't write it, Java will add super () in the first sentence by default ();If you want to call the parent class constructor In the constructor of the subclass, you need to use it in the first line of the constructor of the subclass (except for the comment statement: super (parameter list) method call. This parameter list is consistent with the parameter list declared by the constructor of the parent class. If you do not call it in the subclass constructor, you cannot directly call the parent class constructor. In this case, you create a parent class Object (created by using the parent class constructor with parameters you want to call) and indirectly call the constructor.
2. If the parent class only has a constructor with parameters, does the subclass unconditionally call the constructor with parameters of the parent class?
Sub-classes can call the non-parameter constructor of the parent class unconditionally. To call a constructor with parameters, you must call the constructor explicitly. However, if the sub-classes do not explicitly call the constructor of the parent class, it will call the non-argument constructor automatically added to the parent class compiler. You can see that there is a non-argument constructor in the parent class through decompilation. For example:Class child {public static void main (string ARGs []) {system. out. println ("this is Father class");} Child () {system. out. println ("OK... ") ;}} public class parent extends child {parent (string a, string B) {// super (); // no matter whether this sentence is added or not, sub-classes call the construction method system without parameters of the parent class by default. out. println ("this is child mudi struct method");} public static void main (string ARGs []) {system. out. println ("this is child class"); parent P = new parent ("XX", "XX ");}}Note that Java uses a handle to connect to a new object and automatically recycles garbage. c ++ uses an object variable, its constructor is automatically called when class objects are defined, and the copy constructor is called when values are assigned.
Function, while Java only copies a handle, and the object is not copied.
In Java, parent P; indicates that the variable is not initialized and cannot be used.