**
* Method overloading means that multiple methods with the same name can be written in the class, but these methods have the same parameter type and number of parameters except for the same method name.
* The return value is different from that of the method.
*
* The constructor is a special method. The method name must be the same as the class name.
* The constructor has no return value type and no void.
* The constructor cannot inherit from the parent class.
* Constructor methods can be reloaded. A class can have multiple constructor methods. Different objects depend on different parameters.
* Select an appropriate constructor.
* The constructor cannot be referenced directly by the method name. The new operator must be used.
* You can call another constructor of the current class and its parent class in the constructor, but it must be the first statement in the method body.
If the first constructor in a constructor does not use super to call the constructor of the parent class, the compiler also calls the constructor using the super () statement by default.
Construction method without Parameters
However, if the first sentence of the constructor is to use this to call another constructor in this class, it will not call Super () by default ()
* Use this to reference the constructor of the current class, and use super to reference the constructor of the parent class.
* There are three Super cases:
The method used to access the override method in the parent class.
Used to access the constructor in the parent class,
Used to access hidden member variables in the parent class.
* Assigns an initial value to the variable in the method of the class.
*/
Next we will create a parent class
1 import javax. swing. spring; 2 3 Public class overloading {4 5/** 6 * method overloading means that multiple methods with the same name can be written in the class, except for the same method name, the parameter type, number of parameters 7 *, and return value of the method are different. 8*9 * the constructor is a special method. The method name must be the same as the class name. 10 * the constructor has no return value type, and the void11 * constructor cannot inherit 12 * constructor from the parent class. A class can have multiple constructor methods, select an appropriate constructor for different objects based on different 13 * parameters. 14 * the constructor cannot be referenced directly by the method name. The new operator 15 * must be used to call another constructor of the current class and its parent class in the constructor, but it must be the first statement of the method body 16 * use the constructor of the current class to reference it with this, use the constructor of the parent class to reference 17*18 * as the initial value of the variable in the method of the class. 19 */20 */21 public spring D; 22 public overloading () {23 // todo auto-generated constructor stub24 system. Out. println ("I am the frist method! I do not have parameter "); 25} 26 Public void overloading () 27 {28 // todo auto-generated method stub29 system. Out. println (" I am the method 2! I have not return value! "); 30} 31 public overloading (string d) 32 {33 34 35 // d =" 33 "; if you assign a value to D here, then the value of overloading ol2 = new overloading ("3"); will be 36 // overwritten. That is, in the output result, the value of D is 33 rather than 337 system. out. println (d); 38} 39 40 public static void main (string [] ARGs) {41 // class instantiation 42 overloading OL = new overloading (); 43 ol. overloading (); 44 overloading ol2 = new overloading ("3"); 45} 46 47}
Then inherit the parent class from the subclass and call the methods in the parent class:
Public class suboverclass extends overloading {// subclass construction method public suboverclass () {// todo auto-generated constructor stub // call the construction method of the parent class (do not have parameter) super (); // invoke a normal method from father class super. overloading ();
System. out. println ("I Am a constructor stub in subclass. ");} public suboverclass (string c) {// subclass constructor method 2 (have a parameter) Super (" I Am a constructor method and have a parameter. "); system. out. println ("I Am a constructor stub in subclass. "+ C);} public static void main (string [] ARGs) {// No parameter constructor suboverclass sub = new suboverclass (); suboverclass sub2 = new suboverclass ("and I invoke the method from farther class overloading. ");}}
Note the difference between calling common methods and constructor methods when calling the parent class method.