aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes describe and apply the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "describe and apply the "program to an interface" principle" Watch "describe and apply the "program to an interface" principle" New topic
Author

describe and apply the "program to an interface" principle

Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
I finished school in 2000 and never once heard this phrase. I can't make hide nor hair of it and googling just seems to lead me to gibberish and quotes from GOF.

Does "program to an interface" mean having an interface for every class?

Does it mean ignoring the implementation so long as you have a JavaDoc/documentation of what your input and output should be?

I did find something that mentioned (it was for C# though but these prinicples transfer over oop languages) that the author codes more actual Interfaces (does C# have those?) and has other programmers implement them.
Mahesh Kumaraguru
Ranch Hand

Joined: Jun 01, 2005
Posts: 95
Originally posted by Matt Kidd:
...I finished school in 2000 and never once heard this phrase.

Does "program to an interface" mean having an interface for every class?

Does it mean ignoring the implementation so long as you have a JavaDoc/documentation of what your input and output should be?

I did find something that mentioned (it was for C# though but these prinicples transfer over oop languages) that the author codes more actual Interfaces (does C# have those?) and has other programmers implement them.


Program to an interface means do not tie relationships to classes, instead tie them to interfaces. For example, I have a class C1 which is a parameter to method M2 in class C2. I code M2 method as follows :-

M2( C1 c )

I cannot use M2( C1 ) for processing any other class. Instead if an interface I1 is created and which is implemented by class C1, I can change the method signature to

M2( I1 i )

and this will be able to process objects of any class ( including C1 ) which implement this I1 interface.

Originally posted by Matt Kidd:


Does "program to an interface" mean having an interface for every class?



Its desirable to always have classes implement interfaces and public methods of the class are always from interfaces ( this is mentioned in some Java coding standards on the net ), but this programming standard / guideline is not strictly followed in realtime projects.

My 2 cents...

Regards,
Mahesh

Sun Certified Java Associate study group
http://groups.yahoo.com/group/Sun_Certified_Java_Associate/
[ June 07, 2005: Message edited by: Mahesh Kumaraguru ]

Regards,<br />K. Mahesh<br /> <br />SCJP 1.2 Aug-2001 : 79%
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

To expand on Mahesh's post, consider the following code:In the code above, I have explicitly stated what class my collection is in lines 2 & 9 - method2 will return a HashSet, and method1 will only accept a HashSet (or a subset of HashSet). In this case, I have programmed to the class type itself.

This usually works, however it can be annoying if you ever need to change your code. If I decided later that a HashSet is not suitable (for example, if I wanted to know the order items were added to the collection), I would have to change lines 2, 9, and 10 - in all, 4 instances of the class name "HashSet" would need to be changed to something else (possibly ArrayList).

Consider then, what happens when I change the code, such that I am programming to an interface. In this case, I am programming to the Collection interface:

This code, as presented, will behave in exactly the same way as the original code, however I have now programmed to the interface. I was able to do this, since no part of the code relied on anything that was specific to a HashSet.

If I needed to change the code now, such that I use an ArrayList instead of a HashSet, I only need to change the one occurence of HashSet to ArrayList. The remainder of the code will still work.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
Thanks Andrew that helped make Mahesh Kumaraguru explanation more clear. No offense Mahesh but the M1, C2, C1 really confused me. The real code example just made more sense. Some things from school, that I've forgotten the names to, came back...probably it was inheritance.

How often is this practiced in the industry? I recall working on a project where I'd have to find every instance of something being used but I just used a find/search on the code base, but I do see how this would be more prudent/expediant.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Hi Matt
How often is this practiced in the industry?


As with all things, it varies.

Most of the standard frameworks use some form of programing to interface. If you are using EJBs, you will certainly be programming to interfaces.

In the past, programming to interfaces was not given a high priority by busy programming teams, in the same way that unit testing, regression testing, documentation, ... were not given priorities. However it is worth looking at this, and seeing how you can use it. Once you get used to programming to interfaces, you will find it easy to do, and the rewards when you need to maintain code make it very worthwhile.

Regards, Andrew
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
Can you think of any other examples of programming to an interface? no need for code I just want other "basic" examples so I can clarify on my end of just get a better picture because this looks like coding to a superclass/interface and letting the compiler determine the actual class to use.
Mahesh Kumaraguru
Ranch Hand

Joined: Jun 01, 2005
Posts: 95
Hi Matt Kidd,

I had previously given a C1, C2, M2, I1 example because I wanted to give a simple example ( sorry if that wasn't clear ).

Refer http://www.research.umbc.edu/~tarr/dp/lectures/OOPrinciples-2pp.pdf - By By Bob Tarr / University of Maryland Baltimore County
"Principle 3" Page 17 of 31 / Slides 33 to 39.
Its a code example with very good explanation.

Regards,
Mahesh

Sun_Certified_Java_Associate@YahooGroups.com
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
sweet that covers composition as well....
Payam Mehradnia
Greenhorn

Joined: Mar 03, 2011
Posts: 1
Mahesh Kumaraguru wrote:Hi Matt Kidd,

I had previously given a C1, C2, M2, I1 example because I wanted to give a simple example ( sorry if that wasn't clear ).

Refer http://www.research.umbc.edu/~tarr/dp/lectures/OOPrinciples-2pp.pdf - By By Bob Tarr / University of Maryland Baltimore County
"Principle 3" Page 17 of 31 / Slides 33 to 39.
Its a code example with very good explanation.

Regards,
Mahesh

Sun_Certified_Java_Associate@YahooGroups.com


Thanks! very useful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: describe and apply the "program to an interface" principle