This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I don't know the exact reasoning why Java is designed that way, but at a guess, I'd say that it would violate encapsulation.
Assume for a moment that you could include instance variables in an interface. The methods and members of an interface are public. If you implement the interface in a non-abstract class, your class's required instance variable will be public (since you can't reduce the accessibility). That is not so good, so you can't do it.
In good OOP, variables are part of implementation. They should be private. Clients are only supposed to access them through accessor, mutator, and predicate methods. You are not supposed to put implementation in an interface.
Constants are an exception. They do not contain state information. They can provide the client with public information such as Integer.MAX_VALUE or Thread.NORM_PRIORITY that can be helpful in using the interface.
SCJP 1.4, SCWCD in process
Joined: Dec 24, 2004
Can anyone tell me about real life implementation experiences concerning this?
I'm trying to understand how I can really use interfaces and leverage the technology that the Java developers thought were important enough to spend their time developing.
Please understand, I'm not trying to be sarcastic. I just feel that there has to be something that I'm not quite getting yet... after all, I'm still a greenhorn!