aspose file tools
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Programming to interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Programming to interface" Watch "Programming to interface" New topic

Programming to interface

Atah Tabotnjap

Joined: Mar 17, 2008
Posts: 24
I have problems understanding the concept of "Programming to interface". Can someone help me out ?? What exactly does programming to interface mean??
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42954
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.
Fabio Falci Rodrigues
Ranch Hand

Joined: Dec 01, 2007
Posts: 36
A good explanation by Erich Gamma
Design Principles from Design Patterns

Atah Tabotnjap

Joined: Mar 17, 2008
Posts: 24
Thanks a lot man.
I agree. Here's the link:
subject: Programming to interface