• 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

Programming to interface

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have problems understanding the concept of "Programming to interface". Can someone help me out ?? What exactly does programming to interface mean??
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:


getMyData1 returns the type of the concrete implementation, while getMyData2 returns the type of the interface it implements.

The benefit of getMyData1 is that the calling code can do more things with it (all those methods of ArrayList that are not part of List). But there is a big drawback: by declaring the kind of List you're using as the return type, you've exposed it to all code that calls this method. If you later decide that using an ArrayList wasn't such a good choice after all -you'd much rather use a LinkedList-, well, you can't. Every piece of code that calls getMyData1 needs to be changed so that it no longer assumes it's getting an ArrayList.

You may not think that's a big deal if your own code is the only one calling getMyData1, but what if this was the public API of a library that other people use? You'd be out of luck.

If you had used getMyData2, you could have easily changed the type of myData from ArrayList to LinkedList, and noone would have been affected by that. It's rare that the calling method would need to it's getting an ArrayList anyway - just about all the interesting methods are part of List, so it's sufficient to declare that.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good explanation by Erich Gamma
Design Principles from Design Patterns
 
Atah Tabotnjap
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot man.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic