Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces...

 
Marcus Laubli
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces look to me like they're a neat method to help during the analysis phase. Getting all of the "ducks in a row", organizing classes, etc.

I also understand the "what, not how" contract and the getXXX and setXXX paradigm to hide interface variables.

I'm a little confused though on why instance variables cannot be defined in the interface.

I'm assuming that if I create an abstract method in the implementing class, that I can also send the responsibility of implementing that specific method down to the subclass, correct?

What am I missing? Can someone help me out here?
 
Joseph Maddison
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Hope this helps,
jdmaddison
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the reasoning is based on OOP principles.

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.
 
Marcus Laubli
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Marcus
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link1
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This links can help you to understand Interfaces:

Link1
Link2
Link3
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic