Java class constructor, java class Constructor
The subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, then, the constructor of the parent class must be explicitly called using the super keyword in the constructor of the subclass and configured with an appropriate list of subordinate classes.
If the parent class has no parameter constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass. If the super keyword is not used, the system automatically calls the non-argument constructor of the parent class.
Public class InheritanceDemo {public static void main (String args []) {// SubClass SC = new SubClass (); SubClass sc2 = new SubClass (200); sc2.eat ();} public static class SuperClass {private int n; SuperClass () {System. out. println ("SuperClass ()");} SuperClass (int n) {System. out. println ("SuperClass (int n):" + n); this. n = n;} void eat () {}} public static class SubClass extends SuperClass {private int n; SubClass () {// super (300); System. out. println ("SuperClass");} public SubClass (int n) {// super (300); System. out. println ("SubClass (int n):" + n); this. n = n ;}}}