Sunday, 19 June 2016

super() implicitly added in sub-class constructor


If sub-class constructor does not explicitly invoke any super-class constructor, a super() will be automatically added in the first line of sub-class constructor.

class Father {

    public Father() {
        System.out.println("in father!");
    }

    public Father(String wife) {
        System.out.println("in father wife!");
    }

}

class Kid extends Father {

    public Kid() {
        System.out.println("in kid!");
    }

    public Kid(String mother) {
        System.out.println("in kid mother!");
    }

}

/**
 *
 * @author YNZ
 */
public class TestSuper {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Kid kid = new Kid();
        System.out.println("----");
        Kid kid1 = new Kid("mother");
    }

}


output
run:
in father!
in kid!

in father!
in kid mother!


so when sub-class doesn't explicitly invoke any super-class constructor, and mean while super class has no non-argument constructor defined, it will cause an compiling error.

No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...