tags:

views:

142

answers:

4

I know it's not possible to define a constructor in a interface. But I'm wondering why, because I think it could be very useful.

So you could be sure that some fields in a class are defined for every implementation of this interface.

For example consider the following message class:

public class MyMessage {

   public MyMessage(String receiver) {
      this.receiver = receiver;
   }

   private String receiver;

   public void send() {
      //some implementation for sending the mssage to the receiver
   }
}

If a define an interface for this class so that I can have more classes which implement the message interface, I can only define the send method and not the constructor. So how can I ensure that every implementation of this class really has an receiver set? If I use a method like setReceiver(String receiver) I can't be sure that this method is really called. In the constructor I could ensure it.

+10  A: 

What you have described ("So you could be sure that some fields in a class are defined for every implementaiton of this interface.", "If a define a Interface for this class so that i can have more classes which implement the message interface, i can only define the send method and not the constructor") is exactly what abstract classes are for.

matt b
omg yes thats true
Roflcoptr
but note that the use case @Sebi describes (calling overloaded methods from parent constructors) is a bad idea as explained in my answer.
rsp
Matt, that's clearly true, but abstract classes suffer from the single-inheritance limitation, which leads people to look at other ways of specifying hierarchies.
CPerkins
This is true and may solve Sebi's immediate problem. But one reason for using interfaces in Java is because you cannot have multiple inheritance. In a case where I cannot make my "thing" an abstract class because I need to inherit from something else, the problem remains. Not that I claim to have a solution.
Jay
@CPerkins while this is true, I am not suggesting that simply using an abstract class will solve Sebi's use case. If anything, it's best to declare a `Message` interface which defines the `send()` method, and if Sebi wishes to provide a "base" class for implementations of the `Message` interface, then provide an `AbstractMessage` as well. Abstract classes shouldn't take the place of interfaces, was never attempting to suggest so.
matt b
Understood, Matt. I wasn't arguing with you, more pointing out that it's not a *complete* replacement for what the op wants.
CPerkins
A: 

Dependencies that are not referenced in an interfaces methods should be regarded as implementation details, not something that the interface enforces. Of course there can be exceptions, but as a rule, you should define your interface as what the behavior is expected to be. Internal state of a given implementation shouldn't be a design concern of the interface.

Yishai
A: 

See this question for the why (taken from the comments).

If you really need to do something like this, you may want an abstract base class rather than an interface.

JSBangs
+1  A: 

An interface defines a contract for an API, that is a set of methods that both implementer and user of the API agree upon. An interface does not have an instanced implementation, hence no constructor.

The use case you describe is akin to an abstract class in which the constructor calls a method of an abstract method which is implemented in an child class.

The inherent problem here is that while the base constructor is being executed, the child object is not constructed yet, and therfore in an unpredictable state.

To summarize: is it asking for trouble when you call overloaded methods from parent constructors, to quote mindprod:

In general you must avoid calling any non-final methods in a constructor. The problem is that instance initialisers / variable initialisation in the derived class is performed after the constructor of the base class.

rsp