• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using interface in getter and setters.

 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic