4

I have tried the following:

public class OuterClass
{
    abstract class abstract AbstractInnerClass
    {
        final Object member;
        public Abstract InnerClass(Object member) { this.member = member; }
    }
    public class InnerClass extends AbstractInnerClass { }
}

However, this gives me a compile fail:

Parent class has no 0-argument constructor for implicit construction

However I can change InnerClass to:

public InnerClass extends AbstractInnerClass
{
    public InnerClass(Object member) { super(member); }
}

Another change I can make indicates at least some constructors inherit:

public class AbstractInnerClass
{
    Object member;
    public AbstractInnerClass() { }
    public AbstractInnerClass(Object member) { this.member = member; }
}

Thing is, I do not care about the 0-argument constructor and don't foresee ever having any use for it. I tried to make an abstract constructor:

public class AbstractInnerClass
{
    Object member;
    public abstract AbstractInnerClass(Object member) { this.member = member; }
}

Unfortunately the above also results in a compile fail:

Constructors cannot be abstract

Is there no way to inherit constructors?

3

If you define a non zero arguments constructor the default constructor (0 arguments) is not generated. For example if you have a class

public class Person {
    public String name { get; set; }

    public Person(String name) {
        this.name = name
    }
}

You can not create an instance of that class using default constructor like:

Person foo = new Person();

You will always get Constructor not defined: [Person].<Constructor>() error. If you would like to be able to create instances of your class using default and custom constructors you need to define 0 arguments constructor by yourself. And then in the child classes 0 arguments constructor will we called at the very beginning if you don't specify which parent constructor should be used (eg. super keyword).

In other words: 0-argument constructor inherits by default. If you want to inherit (execute) functionality from a constructor with arguments, you need to manually call parent constructor from child constructor.

| improve this answer | |
  • I don't want to use the 0-argument constructor in any of my classes. This answer does not respond to what I'm asking. – Adrian Larson Nov 23 '15 at 15:44
  • Updated the answer. Now it should be clear. – steals Nov 23 '15 at 15:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.