This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces..." Watch "Interfaces..." New topic
Author

Interfaces...

Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
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?


Marcus L´┐Żubli, SCJP 1.4, CLP 5.0, SCWCD 1.4 (preparing)
Joseph Maddison
Ranch Hand

Joined: Nov 04, 2004
Posts: 53
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

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Marcus Laubli
Ranch Hand

Joined: Dec 24, 2004
Posts: 116
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

Joined: Jul 28, 2003
Posts: 1844

Link1


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

This links can help you to understand Interfaces:

Link1
Link2
Link3
 
wood burning stoves
 
subject: Interfaces...
 
Similar Threads
Trying to understand interfaces
method and variable declaration in interfaces
Marcus exam 3 q7
Variable scope within an interface and some questions
can Interface Implement and extend?