Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using interface in getter and setters.  RSS feed

 
Vishal Karnik
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to understand a web crawler program in java, wherein an interface was used in getter and setter methods as like below. It would be a great help if anybody can help me understand in which scenarios we set interface in getter/setter methods.




 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37261
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal,
The idea is that your class doesn't care which implementation it gets as long as the interface is implemented. This makes code more flexible.

For example, suppose I have a class that writes to a "database." If I take an interface, I can pass in a mock version for testing or a CSV "database" or one that accesses Oracle.
 
Vishal Karnik
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne,
Thanks for explaining with example.
I read java books to understand oops concepts and execute provided examples. But I get confused/fail to understand the code written by professionals as part of project(like the one mentioned in my first post). This restricts me to reading the same topics(interfaces/classes/oops) again and fear to code and practice simple examples.

Is there any book or guide which can help understand such tricky ways to code.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Karnik wrote:Is there any book or guide which can help understand such tricky ways to code.

"tricky" is certainly the wrong word to use here. In fact, "tricky" code should be avoided. While the use of interfaces may be considered "advanced", it's hardly "tricky".
 
Vishal Karnik
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Bear Bibeault,
But is there is any resources/book which contains advanced oops features explained with examples/scenarios.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!