Constructor Chaining in Java

When you have a class that extends another class in Java, initializing that child class will first call the parent’s initializer, and then the child’s. So if you have:

public class Base {
    public Base() {
        System.out.print("Base  ");
    }
}
public class Derived extends Base {
    public Derived() {
        System.out.print("Derived");
    }
}

The code Derived d1 = new Derived(); will print Base Derived.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>