2

I have a BaseClass in a external jar, it has a constructor setting Implementation class(JerseyClientImpl) to jerseyClient.

public BaseClass(AuthDetails auth, String ID) {
    setListID(D);
    this.jerseyClient = new JerseyClientImpl(auth);
}

I am extending the BaseClass to set my own Implementation class to jerseyClient , but i am getting the error mentioned. Changing the BaseClass to add default constructor is not in my control as i said its an external jar.Can you suggest how can i overcome this error.

skk
  • 151
  • 1
  • 2
  • 9

3 Answers3

2

In Java, if you don't explicitly provide a call to a superclass constructor as the first statement in a constructor, then it will insert an implicit call to the default superclass constructor. If there is no default superclass constructor, then you get the error you have mentioned.

The JLS, Section 8.8.7, states:

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

You must explicitly call the superclass constructor, passing all arguments, with something like this:

public JerseyClientImpl(AuthDetails auth, String ID) {
    super(auth, ID);

    // Rest of constructor code
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

Since BaseClass has a non default constructor, it doesn't have the automatically generated parameterless default contstructor.

Therefore your sub-class can't rely on the default constructor (since it won't be able to call the non-existing default constructor of the base class), so your sub-class must have an explicit constructor that calls the constructor of the base class.

Either a constructor with the same parameters :

public SubClass(AuthDetails auth, String ID) {
    super(auth,ID);
    ...
}

Or a constructor without parameters that gives default values for the base-class's constructor :

public SubClass() {
    super(null,"something");
    ...
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thanks for your response,but the problem is adding super(auth,ID) is calling the BaseClass constructor and setting JerseyClientImpl(auth) but i need ProxyClientImpl to be passed from subclass. public class MySubscribers extends Subscribers{ public MySubscribers(AuthenticationDetails auth,String listID) { //super(auth, listID); forcing me to use parent constructor, but i want my impl to be passed. //this.jerseyClient = new ProxyClientImpl(auth); } – skk Aug 08 '14 at 18:38
  • @skk You probably can't set `this.jerseyClient` directly from your sub-class's constructor, since it's probably a private member, but if the super class has a setter method, you can put in your constructor a call to `setJerseyClient(new ProxyClientImpl(auth));` after the call to the `super(auth,ID)`. – Eran Aug 08 '14 at 21:30
1

First of all, if you are writing some parameterized constructor in a class...the default no arg constructor of the class does not exist anymore.

And, when you try to create constructor of its child class, the no-arg constructor of parent class is always called first.If it doesn't exist, you get compiler error.

So, define the no arg constructor in your parent class, OR just call the parameterized constructor of parent class with some value inside the child class constructor.

HoneyBee
  • 29
  • 8