Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

There must be a good reason why Java designers didn't allow any state to be defined in interfaces . Can you please throw some light on this aspect of design decision ?

share|improve this question

closed as not constructive by Jesse C. Slicer, Glenn Nelson, Yusubov, Martijn Pieters, Dynamic Jan 27 '13 at 22:55

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

17  
No, you get it wrong. Like in "there must be a good reason, why bicyle designers didn't allow four wheels on **bi*cycles". Because a bicycle, by definition, has only two wheels, that's why. Anything that has not two wheels is not a bicycle. –  Ingo Jan 26 '13 at 18:48
3  
Are there languages where interfaces are allowed to have state? –  Zachary Yates Jan 26 '13 at 19:10
8  
I can't even think what state would mean in the context of an interface. –  pdr Jan 26 '13 at 19:19
    
Sounds like the OP wants multiple inheritance. –  MrFox Jan 28 '13 at 19:10

6 Answers 6

up vote 27 down vote accepted

An interface is a contract specifying what its implementer promises to be able to do. It does not need to specify state because state is an implementation detail and only serves to constrain implementers in how this contract is fulfilled. If you want to specify state you might want to rethink you use of interfaces and look at abstract base classes instead.

share|improve this answer
    
Good answer (+1): an interface only defines a contract in the form of certain method signatures, and does not provide any implementation (method bodies and state). –  Giorgio Jan 26 '13 at 19:03
    
@Giorgio this does not follow. Also consider extension methods, as proposed for java. They need a default implementation. –  Ingo Jan 26 '13 at 19:08
    
@Ingo: From a modelling point of view, what is the difference between an interface with extension methods and an abstract class? –  Giorgio Jan 26 '13 at 20:03
    
@Giorgio I don't understand that question. Extension methods are a provision to not break existing software when an interface is extended - i.e. new methods added. The only way to achieve this is to provide a default implementation, so that all previous clients still implement the interface even if they have no own implementation of the new method. –  Ingo Jan 26 '13 at 20:06
3  
@Ingo: What I mean is that interfaces with extension methods are no longer interfaces (at least not in the usual sense of the word) but some kind of abstract classes. They are used as a workaround for extending existing interfaces without breaking backward compatibility but from the point of view of what they model they are very similar to abstract classes (classes in which some methods are implemented, and others are only declared). Of course we can always change a concept and then adapt the meaning of the words afterwards, but this seems a very ad-hoc solution to me. –  Giorgio Jan 26 '13 at 20:14

Because interfaces are meant to specify an abstraction.

A good abstraction should be as simple as possible, which means specifying as little as possible about implementation details. State is an implementation detail, and as such shouldn't be included in your abstraction.

Java designers therefore made a pretty good decision to avoid including state in interfaces.

Alternatives to consider if you ever feel like that you want state in an interface:

  • Use composition to plug together objects that have do state and implement your desired interfaces.
  • Use an abstract base class (but be aware that this mixes interface and implementation and can get you into trouble if you are not careful)

An interesting video on this topic is "Simple made easy" by Rich Hickey. They key point is that if you mix things together that should be kept separate, you get into a mess.....

share|improve this answer

An interface by its nature is simply a specification of what an object should look like, by its very definition it doesn't have state, its not just java its any language.

share|improve this answer

Because with state it would have been an abstract class; and since there is multiple inheritance allowed for interfaces => Java would have supported full multiple inheritance => diamond problem.

share|improve this answer
    
Interfaces with extension methods do support multiple inheritance and therefore the diamond problem can occur. –  Giorgio Jan 28 '13 at 9:40

Just adding - an interface, by definition, doesn't have state. That's the definition of what an interface is, and if it had state then it wouldn't be an interface any more.

If you need to use state then an interface is the wrong tool for the job you want to do. Maybe you'd be better off asking another question (and maybe on SO itself rather than here) describing exactly what the problem you're encountering that is causing you to ask this question is, and you may get more help in finding a solution.

share|improve this answer
    
+1 solve the right problem with the right tool. –  Nadir Sampaoli Jan 27 '13 at 18:18

Think of an interface as being the specification for building a class.

Of course the class can have more in it than just what is in the interface specifies.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.