views:

68

answers:

3

Hi,

I have an abstract class A that define abstract methods. This means that, for a class to be instanciable, all the abstract method have to be implemented.

I'd like all my subclasses to implement a constructor with 2 ints as parameters.

Declaring a constructor defeats my purpose, as I want the constructor defined in subclasses and I don't know anything about the implementation. Moreover I cannot declare a constructor as being abstract;

Is there a way to do this ?

Example of what I want:

Lets say that I am defining the API of a Matrix class. In my problem, Matrix cannot change their dimensions.

For a Matrix to be created, I need to provide its size.

Hence, I want all my implementors to provide the constructor with the size as a parameter. This constructor is motivated by the problem, not by an implementation concern. The implementation can do whatever it wants with these, provided that all the semantic of the methods are kept.

Let's say I want to provide a basic implementation of the invert() method in my abstract class. This method will create a new matrix with this inverted dimensions. More specifically, as it is defined in the abstract class, it will create a new instance of the same class as this, using a constructor that takes to ints. As it does not know the instance it will use reflection (getDefinedConstructor) and I want a way to waranty that I'll get it and that it will be meaningfull for the implementation.

+4  A: 

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?

Jon Skeet
Lets say that I am defining the API of a Matrix class. In my problem, Matrix cannot change their dimensions.For a Matrix to be created, I need to provide its size.Hence, I want all my implementors to provide the constructor with the size as a parameter. This constructor is motivated by the problem, not by an implementation concern. The implementation can do whatever it wants with these, provided that all the semantic of the methods are kept.
dodecaplex
@dodecaplex: But what if you want to create a `FixedSizeMatrix` implementation which is *always* 10 x 10? You can't *call* constructors polymorphically anyway, so why are you trying to restrict the implementation?
Jon Skeet
Well then the implementation will not conform to my API... If it has good reason to do it, then, it will provide a zero arg constructor and a 2 args constructor that will raise an Exception if args are not 10x10.This means that I will still be able to create an empty Matrix of the same implementation and of the same size (without knowing the effective implementation), but I'll get an exception if I try to use this implementation for non 10x10 Matrixes.
dodecaplex
I do not want to restrict the implementation, but to properly define the contract it has to fullfil.The only thing is why shouldn't I be able to include a constructor in the contract ?
dodecaplex
@dodecaplex, you could easily declare a constructor in the abstract class that took two ints, and then a no-arg constructor for `FixedSizeMatrix` which looks like `public FixedSizeMatrix() { super(10, 10); }`, like Jon said. APIs are less about constructors (how you build an object) than they are about methods (how you use an object).
matt b
+1  A: 

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.

Tim Bender
Well, if I'm defining a Point in a n-dimension space, I know for sure that the point cannot exist in a m-dimension space (m != n). Hence, It seems to me that the dimension of the space is an INHERENT attribute of the Point, regardless of the point implementation. As far as I can tell this is encapsulation and data abstraction.If the dimension is NECESSARILY known for each instance to be created it seems natural to REQUIRE all implementations to provide a constructor that takes this argument.
dodecaplex
@dodecaplex, Ahh, a point, great example. Let us suppose you are talking about a 2-dimensional point. Well, sure, I can use an x and y value to represent it. Or, I can use a radians/degrees value and a distance value to represent it. Just one example. There could be other ways. You only think you know how one might implement. Anyway, by definition, if you are trying to enforce a representation you are not using data abstraction.
Tim Bender
A: 

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 ) ;
    }
}
emory