How can I force a Constructor to be defined in all subclass of my abstract class

You could try something like below. The constructor will thrown an exception if the implementing class does not have a constructor with the appropriate arguments.

This is silly. Compare OK and Bad. Both classes are the same, except that OK meets your requirement and thus passes the runtime checks. Thus enforcing the requirement promotes counter-productive busy work.

A better solution would be some sort of Factory.

abstract class RequiresConstructor
{
    RequiresConstructor( int x, int y ) throws NoSuchMethodException
    {
    super();
    System.out.println( this.getClass().getName() ) ;
    this.getClass(). getConstructor ( int.class , int.class ) ;
    }

    public static void main( String[] args ) throws NoSuchMethodException
    {
    Good good = new Good ( 0, 0 );
    OK ok = new OK ();
    Bad bad = new Bad ();
    }
}

class Good extends RequiresConstructor
{
    public Good( int x, int y ) throws NoSuchMethodException
    {
    super( x, y ) ;
    }
}

class OK extends RequiresConstructor
{
    public OK( int x, int y ) throws NoSuchMethodException
    {
    super( x, y ) ;
    throw new NoSuchMethodException() ;
    }

    public OK() throws NoSuchMethodException
    {
    super( 0, 0 ) ;
    }
}

class Bad extends RequiresConstructor
{
    public Bad() throws NoSuchMethodException
    {
    super( 0, 0 ) ;
    }
}

A little bit late, but...

Just create a default constructor in your class which is always called as super constructor. In this default constructor you can check all defined constructors with reflection on it's own class object (which is then not the abstract super class but the concrete subclass). If the constructor you want to be implemented is missing, throw a runtime exception.

I'm not a great friend of reflection because it has the taste of hacking through the back door, but sometimes it helps...

Have a look at this example:

import java.lang.reflect.Constructor;

public abstract class Gaga {
  public Gaga() {
    boolean found = false;
    try {
      Constructor<?>[] constructors = getClass().getConstructors();
      for (Constructor<?> c : constructors) {
        if (c.getParameterTypes().length==2) {
          Class<?> class0 = c.getParameterTypes()[0];
          Class<?> class1 = c.getParameterTypes()[1];
          if ( (class0.getName().equals("int") || class0.isAssignableFrom(Integer.class))
              &&  (class1.getName().equals("int") || class1.isAssignableFrom(Integer.class)) )
            found = true;
        }
      }
    } catch (SecurityException e)
    {
      found = false;
    }

    if (!found)
      throw new RuntimeException("Each subclass of Gaga has to implement a constructor with two integers as parameter.");

    //...
  }

}

And a test class:

public class Test {
  private class Gaga1 extends Gaga {
    public Gaga1() { this(0, 0); }
    public Gaga1(int x, Integer y) { }
  }

  private class Gaga2 extends Gaga {

  }

  public static void main(String[] args)
  {
    new Gaga1();
    new Gaga1(1, 5);
    new Gaga2();
    System.exit(0);
  }
}

In the main function the objects of Gaga1 will be created, but the creation of Gaga2 will throw a runtime exception.

But you can't be sure that this constructor is called - you can't even ensure that it is doing the things you want.

This test is only useful if you're working with reflection.


If you need to define in your interface the internal representation that implementing classes will use, then you are just doing it wrong. Please go read about encapsulation and data abstraction.

If your abstract implementation relies on certain implementation details, then they belong to that abstract class. Meaning, the abstract class should define a constructor which allows it to initialize the internal state needed to allow the abstracted methods to work.

Generally, constructors are intended to create an instance of a class by providing some details of the initial state of that object instance. This does not mean that the instance being constructed should copy a reference to each individual argument as is often the case in most software I see. Therefore, even if Java did offer a construct for forcing the implementation of certain Constructor signatures on subclasses, those subclasses could easily discard the arguments.


You can't force a particular signature of constructor in your subclass - but you can force it to go through a constructor in your abstract class taking two integers. Subclasses could call that constructor from a parameterless constructor, passing in constants, for example. That's the closest you can come though.

Moreover, as you say, you don't know anything about the implementation - so how do you know that it's appropriate for them to have a constructor which requires two integers? What if one of them needs a String as well? Or possibly it makes sense for it to use a constant for one of those integers.

What's the bigger picture here - why do you want to force a particular constructor signature on your subclasses? (As I say, you can't actually do this, but if you explain why you want it, a solution might present itself.)

One option is to have a separate interface for a factory:

interface MyClassFactory
{
    MyClass newInstance(int x, int y);
}

Then each of your concrete subclasses of MyClass would also need a factory which knew how to build an instance given two integers. It's not terribly convenient though - and you'd still need to build instances of the factories themselves. Again, what's the real situation here?