16

My question is simple: is there any advantage of using interfaces if they are implemented by a single class ?
I always thought that interfaces are good only when there are multiple implementations of that interface.

Thanks.

Heisenbug
39.3k30 gold badges139 silver badges192 bronze badges
asked Jun 30, 2011 at 10:06
3
  • 2
    I prefer stackoverflow answers because there are many good programers that will give you good answers. Commented Jun 30, 2011 at 10:10
  • 3
    possible duplicate of How are Java interfaces actually used? Commented Jun 30, 2011 at 10:11
  • @ Jigar Joshi: good link, thanks. Commented Jun 30, 2011 at 10:14

6 Answers 6

12

In a word: no. The contract that an interface means can be specified directly in your only class.

If you are clear that you won't need another implementation of the same methods in the future, you can avoid defining an interface.

Of course, the issue here is "in the future" clause. If the project is small, without a long development/upgrade period, and well defined, you can be almost sure about what will be needed in the future.

If the project is long as is probably that it will be subjected to changes, then you will have to factor in:

  • probability that you finally need an interface.
  • probability that you know now what methods the interface will need in the future.
  • cost of doing the interface now vs. refactoring in the future.
answered Jun 30, 2011 at 10:13
Sign up to request clarification or add additional context in comments.

3 Comments

never thought at this. Thanks
I agree with you. It's a non-sense implementing interfaces if we need only an implementation of the same methods. But it's an advantage when you have many classes, many methods, and changes to do. Imagine that without interfaces...tons of lines of code to rewrite! :D
have a look at my answer. There is at least one situation that even without more implementation of an interface, justify the presence of the interface itself.
6

Consider that there may only be one implementation now, but more implmentation may come along in the future. Also, by using an interface you can avoid creating a dependency on the concrete class by only using the interface. Then, later on, if you decide to change the implementation, you can create a new one which implements the interface without adverse affect to the other code which only relies on your interface.

Consider:

public interface SomeInterface {...}
public class AConsumerOfSomeInterface {
 private SomeInterface service;
 public AConsumerOfSomeInterface(SomeInterface theImplementation) {
 service = theImplementation;
 }
 //some code which uses the service
}

Now you can swap implementations at a future date without ever changing this class. Because this is easy to do, it makes sense to use interfaces even when you don't think you'll need one.

answered Jun 30, 2011 at 10:13

Comments

2

Interfaces are a way of decoupling seperate software components. Sure, you may use an Oracle data base to store all your data today so why not dispense of all your DAO interfaces?

The answer is that strongly coupling to anything may come back and bite you in the future. What if next year you want to use some cloud service to store data? If you coded to a DAO interface you can simply introduce a new cloud implementation and plug it straight in. Your app is loosely coupled so doesn't need to change.

answered Jun 30, 2011 at 10:12

Comments

2

is there any advantage of using interfaces if they are implemented by a single class ?

If your interface it's implemented by only one class, and you are 100% sure that you will NEVER feel the needs to add another implementation, then there are no advantages.

Anyway I think there is another situation to keep in mind. Suppose you have a class that do something more than what you want to expose to another client class. Than an interface could be used to limit the methods of your class that a client can call:

public interface clientInterface{
 public foo();
 public bar();
}
public class yourClass implements clientInterface{
 public foo(){}
 public bar(){}
 public doSomethingElse(){}
}
public class clientSideFactory{
 public clientInterface getClass(){
 return new yourClass();
 }
}
public class internalSideFactory{
 public clientClass getClass(){
 return new yourClass();
 }
}
}

In the situation illustrated above, even if you have a single implementation (clientClass)of the interface clientInterface, this is useful to be returned to a client class that should only see a limited part of your class (limiting the visibility of your class complete implementation).

answered Jun 30, 2011 at 10:19

1 Comment

Even if you are guaranteed never to have another implementation, there are still many times where it makes sense to create an interface as it can make testing of consumers of the concrete implementation of the interface much easier, since they can use dependency injection to substitute a more test-friendly implementation of the interface.
2

I would like to add another point: Interface helps in Proxy pattern.

In a web application this proxy helps a lot for abstraction. I am new in web application, but usually I design my service layer by interface. In a web application build with Wicket I have, say, an interface Afrodite and a class AfroditeImpl as service layer initialized from applicationContext as spring bean. Then we can get all the methods defined in Afrodite and implemented in AfroditeImpl from all the web pages by

AfroditeApplication application = (AfroditeApplication)getApplication();
Afrodite afrodite = application.getAfrodite();
// Now call the service layer's methods by afrodite

The outline is :

public interface Afrodite {
}
public class AfroditeImpl implements Afrodite {
}
public class AfroditeApplication extends WebApplication {
 Afrodite afrodite;
 public Afrodite getAfrodite() {
 return afrodite;
 }
 @Override
 public void init() { 
 super.init();
 ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 
 afrodite = (Afrodite) applicationContext.getBean("afrodite");
 }
}

Hope this helps.

answered Jun 30, 2011 at 10:36

Comments

1

Ask yourself if there is a real possibility there will be more than one implementation in near future.

You can add interface easily when neded, sometimes several interfaces.

The only case when I create interfaces even for single class is to distinguish API for common usage and special-purpose API (testing, monitoring e.g.).

answered Jun 30, 2011 at 10:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.