how to inherit Constructor from super class to sub class

Anil picture Anil · Feb 23, 2010 · Viewed 112.9k times · Source

How to inherit the constructor from a super class to a sub class?

Answer

Andrew Hare picture Andrew Hare · Feb 23, 2010

Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass.

Here is an example of how this works:

class Foo {
    Foo(String str) { }
}

class Bar extends Foo {
    Bar(String str) {
        // Here I am explicitly calling the superclass 
        // constructor - since constructors are not inherited
        // you must chain them like this.
        super(str);
    }
}