1

I have a class:

public abstract class Response<T> {
    private boolean isSucessfull;
    private T data;

    public Response() {
        throw new IllegalArgumentException();
    }

    public Response(boolean isSucessfull, T data) {
        this.isSucessfull = isSucessfull;
        this.data = data;
    }

    ...
}

And other classes extend it with some specific types:

public class PlainResponse extends Response<byte[]>{
    //
}

The problem occurs when I try to create an instance of PlainResponse: new PlainResponse(success, responseData)

constructor PlainResponse in class PlainResponse cannot be applied to given types;
required: no arguments
found: boolean,byte[]

As you can guess, I'm not an expert in java. But I've expected, that my class will use the constructor of it's super class.

How can I achieve it?
Thanks.

1
  • Note you wouldn't have run into this problem if you didn't have a no-argument constructor in Response in the first place. Seeing as it's impossible to actually call without raising an exception, you would be better off simply not defining it in the first place. Then the compiler can require PlainResponse have a constructor that calls the parent (boolean, T) constructor. Commented May 14, 2015 at 5:50

3 Answers 3

4

But I've expected, that my class will use the constructor of it's super class.

This is not the case. Constructors are not inherited. Often subclasses are created in a very different way than its parent class or need to impose additional invariants on its member variables, so in general letting constructors be inherited would not be a good idea.

So you will have to define a constructor

public PlainResponse(boolean isSucessfull, byte[] data) {
    super(isSuccessfull, data);
    // optionally more work
}

in your PlainResponse class and all other subclasses of Response.

If you do not define a constructor in a class, Java will create a default constructor with no arguments for you. This is the reason for the specific error message you are getting.

Sign up to request clarification or add additional context in comments.

1 Comment

Not quite sure about the first assertion. All member variables have initial values in Java; and since you could always write a constructor consisting only of super(same-arguments), it's "possible" that the language could have decreed that constructors could be inherited and just call the superclass constructor. But it's probably not a good idea. It invites too many accidents.
0

OOP don't allow auto inheritance of constructors form parent class to child class. That's why it is ok if this error is given

If you need to call that constructor of parent class declare a construction with parameter in the child class and call super(arg1,arg2) from it.

public class PlainResponse extends Response<byte[]>{

    public PlainResponse(boolean isSucessfull, byte[] data)
    {
        super(isSucessfull,data);
    }
}

Comments

0

The problem is constructor will not be inherited in sub class, so you get a default constructor which has no arguments. If the sub class to call the parent class's constructor, then you need do it's own constructor.

public class PlainResponse extends Response<byte[]>{
   public PlainResponse (boolean isSucessfull, T data) {
        super(isSucessfull,data);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.