Home>

In this case, the execution result is 1234.
I learned that superclass constructors are always called before subclasses.
If that rule is applied, the constructor does not start from subclass ④,
The default constructor of super ();(or super (String val);) is added to the first line of the B class, and start from ① (or ②)
The execution result appears to be 11234 (or 14234).
If a subclass is specified as super ();is it out of the rule condition?

Applicable source code
public class main {
  public static void main (String [] args) {
    new B ("4");
  }
}
class A {
  public A () {// ①
    System.out.println ("1");
  }
  public A (String val) {// ②
    this ();
    System.out.println (val);
  }
}
class B extends A {
  public B () {// ③
    super ("2");
    System.out.println ("3");
  }
  public B (String val) {// ④
    this ();
    System.out.println (val)
  }
}
  • Answer # 1

      

    I learned that superclass constructors are always called before subclasses.
      If a subclass is specified as super ();is it out of the rule condition?

    I think it's generally correct. If you write down the rules more strictly:

    If there is asuper (argument list);description in the constructor implementation of the subclass, the corresponding constructor of the superclass is called at the processing timing.

    In cases other than the above (superkeyword not used), callsuper ();, that is, "super class argumentless constructor" at the top of the subclass constructor.

    class A {
      // ①: If you do not define a no-argument constructor ...
      public A (String val) {// ②
        System.out.println (val);
      }
    }
    class B extends A {
      public B () {// ③
        // compile error trying to call ① implicitly here
        System.out.println ("3");
      }
      public B (String val) {// ④
        this ();
        System.out.println (val);
      }
    }

  • Answer # 2

    Super () is implicitly supplemented only when there is no super or this.
    The calling order of constructors is 4 ⇒ 3 ⇒ 2 ⇒ 1.

      

    In this case, the execution result is 1234.

    This is inconsistent with the previous claim.

    class Main {
        static void func1 () {
            System.out.println (1);
        }
        static void func2 () {
            func1 ();
            System.out.println (2);
        }
        static void func3 () {
            func2 ();
            System.out.println (3);
        }
        static void func4 () {
            func3 ();
            System.out.println (4);
        }
        public static void main (String [] args) {
            func4 ();
        }
    }

    ResultsWandbox

    1
    2
    Three
    4

  • Answer # 3

      

    Superclass constructors are always called before subclasses

    This is

    "The process coded in the subclass constructor can only be executed after the superclass constructor has finished"

    I hope it will be understood as

    .

    Also,super ()is executed implicitly even if the constructor does not explicitly call other constructors or superclass constructors at the beginning of the class. , Always the above situation.
    However, if another constructor of the class is called in the constructor such asthis (),super ()is executed prior to the constructor making the call. this is not allowed.

    The execution order of Java class constructors is such a specification.