inheritance - Is it possible to call both default and parameterized constructors of SubClass and SuperClass for a particular instance(parameterized) in Java? -
i'm trying below scenario:
public class superclass { public superclass(){ system.out.println("super constructor"); } public superclass(int i){ this(); system.out.println("parameterized super constructor"); } } public class subclass extends superclass{ public subclass(){ system.out.println("sub constructor"); } public subclass(int i){ super(i); /* need call **this()** here .. possible? */ system.out.println("parameterized sub constructor"); } } public class inheritance { public static void main(string[] args) { subclass sub=new subclass(5); } }
how call both default , parameterized constructors case ?
if have functionality in non-parameterized constructor need call both, i'd recommend move out of there a, example, private void init()
function both constructors can call.
Comments
Post a Comment