This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Interfaces...

 
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?
 
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
 
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
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link1
 
Krishna Srinivasan
Ranch Hand
Posts: 1880
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
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic