Constructor in java inheritance
Constructor in inheritance 1. The constructor of the base class must be called during the constructor of the subclass. 2. Subclass can use super (argument_list) in its own constructor to call the constructor of the base class. 2.1 use this (argument_list) to call another constructor of this class. 2.2 If super is called, it must be written in the first line of the subclass constructor. 3. If the constructor of the base class is not displayed in the constructor of the subclass, the system calls the non-parameter constructor of the base class by default. 4. If the subclass constructor does not show that the base class constructor is called, and the base class does not have a constructor without parameters, a compilation error occurs. Copy the code class SuperClass {private int n; // SuperClass () {// System. out. println ("SuperClass ()"); //} SuperClass (int n) {System. out. println ("SuperClass (int n)"); this. n = n ;}} class SubClass extends SuperClass {private int n; SubClass () {super (300); System. out. println ("SuperClass");} SubClass (int n) {System. out. println ("SubClass (int n):" + n); this. n = n ;}} public class TestSuperSub {public static void main (String args []) {// SubClass SC = new SubClass (); subClass sc2 = new SubClass (200 );}