wood burning stoves*
The moose likes Beginning Java and the fly likes importance of the interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "importance of the interface" Watch "importance of the interface" New topic
Author

importance of the interface

Dan Bromberg
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
Though I know it is somehow core to Java, I never really appreciated the importance of the interface. After all, since you have to supply the code to implement the interface what is it really buying you?
Dan


Thanks in advance to all who reply,
Dan
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Dan Bromberg wrote:After all, since you have to supply the code to implement the interface what is it really buying you?
Dan

You yourself have given the answer. Since the classes that implements the interfaces, must implement its methods, interfaces are used as contracts in communication between different classes.

It conceals the concept of implementation - Set interface does the job of a Set regardless of what kind of Object (HashSet, TreeSet, etc) its pointing to.

And have you heard of Marker interfaces that don't have any methods defined?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

John Jai wrote:And have you heard of Marker interfaces that don't have any methods defined?

Yeah, but marker interfaces are a bit of an anti-pattern and needless in the era of annotations. (Though the legacy ones are likely here to stay.) I don't think that they're a good example of what interfaces should be used for.

Imagine also an interface such as HttpServletRequest. By making this an interface, any servlet container, be it Tomcat, Resin, Websphere, Jetty, Weblogic, Glassfish, and so on, can implement the interface and we, as application developers, don't need to care who or what provided the implementation.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

This question has been ask many, many times. search through this forum, and i'm sure you'll find many posts on it.

briefly, most of the time code is developed by different teams/departments/companies. You may buy a third party package that gives you a static method called "ObjectCreatedMethod". You know it will return a ConcreteObject that implements YourInterface. You have two options:





now, a year later, the third party vendor says "we have a NEW class that is 100 times better/faster/uses less memory. It still implements the YourInterface"

If you want to use the new version, and you've written code like line #1, you have to re-factor the whole thing.
If you wrote your code like line #2, you don't have to change a thing.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2377
    
  28

The biggest thing that it buys you is flexibility. The interface defines the contract between 2 classes. By having an interface, the calling class is dependent on the interface, not the implementation of the interface. So, later, if you want to have a completely differrent implementations (or maybe you want 2 differrent implementations), you can easily implement a new class without changing the calling class

Another advantage is coding to an interface makes unit testing easier. Let's say class A is dependent on B, and you are unit testing A. If you have a direct dependency between A and B, then you are relying on B giving you the correct data to test A. So, now your unit test is dependent on the internals of B. Maybe B has dependencies on external resources like databases. Now, you need to make sure a database is available to test A, when A has nothing to do with a database Instead if you split B into an interface B and implementation class BImpl, you can always have a mock implementation of B that you can use in your unit test

Also, with the availability of JDK proxies now, it makes it easier to weave proxies into your already implemented classes, if those classes are built to an interface.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

And consider callbacks.

The Arrays.sort() method accepts an instance of a class that implements the Comparator interface. We provide an implementation of that interface that decides how the array comparisons for sorting take place.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Bear Bibeault wrote:
John Jai wrote:And have you heard of Marker interfaces that don't have any methods defined?

Yeah, but marker interfaces are a bit of an anti-pattern and needless in the era of annotations.

Agree....
Dan Bromberg
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
Thanks to all who replied - I do have a better feeling for the benefit of the "contract" concept which I'd read about but had forgotten.
Dan
John Voris
Greenhorn

Joined: Jan 24, 2001
Posts: 7
Just to recap - are annotations part of Java SE now? They are not tied to the various frameworks out there?


jvoris@axs2000.net<br />iSeries AS400 Java
Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
Yes. Annotations are part of Java SE.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mike Okri wrote:Yes. Annotations are part of Java SE.


And have been since 1.5. Were they introduced in some other corner of the Java universe prior to that?
Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
Jeff Verdegan wrote:Were they introduced in some other corner of the Java universe prior to that?

Where did you get the idea that they might have been introduced in some other corner of the Java universe prior to that?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mike Okri wrote:
Jeff Verdegan wrote:Were they introduced in some other corner of the Java universe prior to that?

Where did you get the idea that they might have been introduced in some other corner of the Java universe prior to that?


From here:
John Voris wrote:are annotations part of Java SE now? They are not tied to the various frameworks out there?


Wasn't sure what he meant by "tied to frameworks", but as it was contrasted with "part of SE", I thought maybe they existed in some other flavor of Java previously--or that at least John thought they did--and I was trying to clear that up.
John Voris
Greenhorn

Joined: Jan 24, 2001
Posts: 7
I was thinking they were first introduced in AspectJ. But now that they are part of Java SE, great!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: importance of the interface