Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Programming to an interface

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For OCA Java SE 7, Mala Gupta does not mention "Programming to an Interface" in her book.
Edward Finegan mentions the following points on page 312 of his book.

Programming to an interface is the concept that

1. Code should interact based on a defined set of functionality instead of an explicitly defined object type.
2. It is better for the public interfaces of objects to use data types that are defined as interfaces as opposed to a particular class when possible.
3. When an object is implementing an interface, it is declaring that it has a certain set of functionalities.
4. Many different classes can implement the same interface and provide its functionality.
5. When a method uses an interface as its argument type, it allows any object, regardless of its type, to be used as long as it implements the interface.
6. Programming to an interface is also known as "design by contract."
7. Programming to an interface allows the developer to specify the capabilities or behaviors that are expected, instead of strictly defining an expected object type.

Example of Programming to an interface:


Question: Besides the points listed for "Programming to an Interface",
what additional items should one be aware of with respect to this topic?

Any additional examples of "Programming to an Interface" would be appreciated?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programming to an interface, not an implementation is a good design practice!

Here you'll find some more info about this topic.
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the techniques used to program to an interface
is to write a method that contains a parameter of type interface.

Question:
If a method contains a parameter that is of an interface type,
is this technique automatically considered "programming to an interface?"


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:If a method contains a parameter that is of an interface type, is this technique automatically considered "programming to an interface?"

Of course!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic