Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

importance of the interface

 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12198
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to recap - are annotations part of Java SE now? They are not tied to the various frameworks out there?
 
Mike Okri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Annotations are part of Java SE.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking they were first introduced in AspectJ. But now that they are part of Java SE, great!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic