6

In production code I often see classes defined as follows:

public interface SomeComponent { // Some methods }
public class SomeComponentImpl implements SomeComponent { // Some methods}

public interface SomeComponentV2 extends SomeComponent { // Some methods }
public class SomeComponentV2Impl extends SomeComponentImpl implements SomeComponent { // Some methods }

Why in this case we want to separate the interface and its implementation?

Or put it this way, why is it bad to simply have one base class, and let V2 extend/override V1 as follows:

public class SomeComponent { // Some methods }
public class SomeComponentV2 extends SomeComponent 
{
  // Override methods for reimplementation
  // Add new methods for new features.
}
OneZero
  • 11,556
  • 15
  • 55
  • 92
  • You don't seem to be using `SomeComponentV2` interface anywhere. Is this intentional? Also, are the interfaces and classes empty? Last but not the least, are you asking why use interfaces when you can just use classes? – Chetan Kinger May 29 '15 at 16:29
  • Sometimes there are what are called "marker" interfaces that have no body, but merely identify an object as belonging to a particular category. Serializable is an example. – swingMan May 29 '15 at 16:30
  • See my update. All the classes/interfaces have content. They are not empty markers but do have real code in them. I saw such design in many production systems' source code, so am wondering why. – OneZero May 29 '15 at 16:34
  • This article on the topic is quite good: http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html – Andy Turner May 29 '15 at 16:34
  • @ChetanKinger I agree that the SomeComponentV2 is not being used. But the interfaces do have methods declared in them, the user has not shown that to us for brevity. – vinay May 29 '15 at 16:35
  • @vinay That was not clear before the edit. – Chetan Kinger May 29 '15 at 16:36
  • in the first listing, its useless for SomeComponentV2Impl to implement SomeComponent inteface – itwasntme May 29 '15 at 16:37
  • This is my go-to link for questions like these: http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface/384067#384067 – MadConan May 29 '15 at 17:12

2 Answers2

1

It is a good practice to separate the interface and the implementation of a class because you can easily swap out classes.

Imagine you want to test a application which depends on a web-service which bills you for every request. In addition to have a class which performs real requests to this web-service, you could build a class which implements the same interface but returns fake data to avoid generating costs for every request.

Every time you inherit from a base-class there is a chance that you inherit behaviour you simply don't want to inherit. An interface is a pure contract and gives you the freedom to let you choose a base-class independently of the described advantage.

Stefan Wanitzek
  • 2,059
  • 1
  • 15
  • 29
  • You could extend a class from the actual class as well. Being able to have test doubles is not necessarily an interface thing. Also, Java 8 allows interfaces to have default methods that have a body, are not abstract and get inherited by their implementing classes. Interfaces are no longer just pure contracts. – Chetan Kinger May 29 '15 at 16:56
  • The need to swap a class for another version is extremely rare in my experience. And nothing prevents you to swap a class that has to separate interface declared. – Florian F Mar 15 '21 at 22:35
0

Separating interface from implementation allows to fully use polymorphism. In this way SomeComponentV2Impl will have 3 types - own, base class, and interface. Here you may just use only the interface without caring about it's implementation in further classes. For example:

public void methodInOuterClass(SomeComponent smCmp){
    smCmp.runInterfaceMethods();
}

[Edit: this question appeared in OP question before edites]

Why do we dont use one base class for them all?

Because SomeComponentV2Impl is distinguish from SomeComponentImpl. But if they implement same interface, you will be able to call their implementation from the interface's refference.

itwasntme
  • 1,442
  • 4
  • 21
  • 28
  • it might seem wrong before, but only because of interfaces and classes names made by OP, which were confusing for me. I think the good practice is to name interface as SomethingModel or SmthInteface, then name the first class implementing it as Something – itwasntme May 29 '15 at 21:15
  • Most of the classes don't need polymorphism. I would reserve the trouble of managing two separate files when it is needed. – Florian F Mar 15 '21 at 22:32