Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Object Reference Type

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Statement:
You can store the reference to any object instantiated from any class that implements an interface in a reference variable of that interface type.
I understand that I can do it. What I would like to know is why would I want to do it. What is the advantage to using an interface reference instead of an object one?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, for instance, Collection, Set, List and Map are all interfaces. You might want to work with the methods promised by one of those interfaces, and not care about the exact class that is providing those behaviors. This allows you to write programs depending on the important abstractions rather than the concrete details.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Declaring a variable or parameter to match an interface type allows you to use a variety of classes that implement that interface now and in the future. For example if we write a method like:

then we can pass in any object that implements Set: HashSet, TreeSet, LinkedHashSet, or any other new Sets that come down the road one day. The getSum method can only call the methods defined for Set, none of the special methods added by the various implementing classes, but we can do computeSum with just those methods.
Here's another cute trick with collections. Populate a collection that is efficient for "put" then sort it into another collection.

To get a bit more advanced, we can use interfaces to manage dependencies between packages. For example, we'd like a Panel to depend on a Customer object and not the other way around. And we'd like the Customer to send a message to the Panel when data changes. How can we code the Customer without making the Customer know something about a Panel method that it can call? In the Customer package, we define an interface which interested parties must implement to be notified. The Panel can implement the interface - creating the appropriate dependency on the Customer - and register itself with the Customer to receive messages.
Lemme know if that helps!
[ June 30, 2003: Message edited by: Stan James ]
 
Let me tell you a story about a man named Jed. He made this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic