Why Interfaces are better than Abstract classes
I have recently begun to understand where a lot of the books/articles/blogs I have read recommend Java programmers to use Interfaces and composition and this has come about because recently I have been using a interfaces a lot more. I noticed this has been a definitive change because before I used to always think of using Abstract classes.
I think as you progress as a developer you move away from using inheritance and then start to favour composition. I think the key area of this change is you start to understand the benefit of interfaces and abstract classes and through practice you get better and using them.
I initially could never understand the point of interfaces, the idea of them seemed like extra work. You have to create an interface which has no code in, except some empty methods. I used to think why would I want to do that when I can create an Abstract class with some methods which have code and then all the classes can extend that and get some code for free. This would mean I have to write less code. Interfaces use to seem like a terrible idea, every class that implements it has to write all the code again for each class that implements an interface.
I have discussed before Interfaces Vs Abstract Classes I have also blogged about why you should use interfaces and abstract classes, I didn't really write that much but I linked to a good article http://hoskinator.blogspot.com/2006/05/interfaces-and-abstract-classes-dont.html
I have moved away from using abstract classes as much because I am using interfaces more and the way I am using Interfaces more. Recently I have been using Interfaces to hide change behind. If there is a piece of code which has an algorithm or recently I was writing some code which checked a directory for a file. I was able to use an Interface and check for a certain file. The interface I created had two methods, one method to check the directory and one to perform an action if it found something. By using an interface I was able to write the code to resolve the problem I was trying to fix but also hide the change behind the interface. If later I need to search for a different file or search for files I can make another class that implements the interface and change the method to check the directory and the action and the rest of the code will work just fine. Here is the blog entry if you would like to read about it in more detail
http://hoskinator.blogspot.com/2006/06/function-pointers-in-java.html
I think the change from using Interfaces from Abstract is the change in thought. I have been thinking about trying to write simple code and nothing extra but also where change might come in the future. If I think the code might change, I bash a interface in allowed a developer in the future to add a different implementation behind the interface but without effecting other parts of the code.
I suppose what I am trying to say is that Interfaces allow you to use polymorphism and benefits that associated with it.
What other benefits are there, well I think interfaces encourage Single_responsibility_principle
in your code because you can make interfaces for smaller tasks. When you use abstract classes they often start off this way but then as you extend them with different flavours the classes seem to start growing and sometimes you need to alter the super class and slowly the code seems get harder to maintain and extend. I think abstract classes and the concrete classes which inherit from them do become more difficult to maintain is the different extending classes start to change the focus of the class and they become less cohesive.
One other weakness I find with inheritance and abstract classes is that any methods you want to overridden or to be able to use you have to make public. Although there is no reason why you don't use the extended class with composition but for some reason I don't. With inheritance and the blog about function pointers I pass in the interface. I like this use of interfaces as instance variable, this is where interfaces are really powerful and useful you can use them as a set of behaviors inside a class. The real advantage is classes using the class which contains the interface rather implements it, don't even know there is a interface being used.
Another weakness of Abstract classes is that you can extend any other classes.
Increased dependency. Abstract classes by their very nature create a fundamental dependency with the subclass and the superclass.
Interfaces seem more comfortable when making an interface with just one or two methods. It seems easier to create a small interface and the interfaces I write seem to be a lot more cohesive and focused on one particular task/behavior. The Benefit of small concrete classes is they are not coupled with other code and have few dependencies. This means it's easier to use the concrete class implementing the interface in other parts of the code.
There are situations when Abstract classes are useful and the right choice, they do have advantages that each class can inherit some general methods used by all extended classes. It's just in my experience recently Abstract classes seem a lot harder to maintain and extend, especially with the more classes that extend the Abstract superclass.
I don't know what other people think about this but it seems to me that after starting off using Abstract classes you get a bit more savvy and then start to use Interfaces a bit more and composition. Interfaces seem to make it easier to write code that cohesive and focused on one thing, this has the result of making the code easier to extend, maintain and unit test before another class implements the interface or uses it like an instance variable
I think as you progress as a developer you move away from using inheritance and then start to favour composition. I think the key area of this change is you start to understand the benefit of interfaces and abstract classes and through practice you get better and using them.
I initially could never understand the point of interfaces, the idea of them seemed like extra work. You have to create an interface which has no code in, except some empty methods. I used to think why would I want to do that when I can create an Abstract class with some methods which have code and then all the classes can extend that and get some code for free. This would mean I have to write less code. Interfaces use to seem like a terrible idea, every class that implements it has to write all the code again for each class that implements an interface.
I have discussed before Interfaces Vs Abstract Classes I have also blogged about why you should use interfaces and abstract classes, I didn't really write that much but I linked to a good article http://hoskinator.blogspot.com/2006/05/interfaces-and-abstract-classes-dont.html
I have moved away from using abstract classes as much because I am using interfaces more and the way I am using Interfaces more. Recently I have been using Interfaces to hide change behind. If there is a piece of code which has an algorithm or recently I was writing some code which checked a directory for a file. I was able to use an Interface and check for a certain file. The interface I created had two methods, one method to check the directory and one to perform an action if it found something. By using an interface I was able to write the code to resolve the problem I was trying to fix but also hide the change behind the interface. If later I need to search for a different file or search for files I can make another class that implements the interface and change the method to check the directory and the action and the rest of the code will work just fine. Here is the blog entry if you would like to read about it in more detail
http://hoskinator.blogspot.com/2006/06/function-pointers-in-java.html
I think the change from using Interfaces from Abstract is the change in thought. I have been thinking about trying to write simple code and nothing extra but also where change might come in the future. If I think the code might change, I bash a interface in allowed a developer in the future to add a different implementation behind the interface but without effecting other parts of the code.
I suppose what I am trying to say is that Interfaces allow you to use polymorphism and benefits that associated with it.
What other benefits are there, well I think interfaces encourage Single_responsibility_principle
in your code because you can make interfaces for smaller tasks. When you use abstract classes they often start off this way but then as you extend them with different flavours the classes seem to start growing and sometimes you need to alter the super class and slowly the code seems get harder to maintain and extend. I think abstract classes and the concrete classes which inherit from them do become more difficult to maintain is the different extending classes start to change the focus of the class and they become less cohesive.
One other weakness I find with inheritance and abstract classes is that any methods you want to overridden or to be able to use you have to make public. Although there is no reason why you don't use the extended class with composition but for some reason I don't. With inheritance and the blog about function pointers I pass in the interface. I like this use of interfaces as instance variable, this is where interfaces are really powerful and useful you can use them as a set of behaviors inside a class. The real advantage is classes using the class which contains the interface rather implements it, don't even know there is a interface being used.
Another weakness of Abstract classes is that you can extend any other classes.
Increased dependency. Abstract classes by their very nature create a fundamental dependency with the subclass and the superclass.
Interfaces seem more comfortable when making an interface with just one or two methods. It seems easier to create a small interface and the interfaces I write seem to be a lot more cohesive and focused on one particular task/behavior. The Benefit of small concrete classes is they are not coupled with other code and have few dependencies. This means it's easier to use the concrete class implementing the interface in other parts of the code.
There are situations when Abstract classes are useful and the right choice, they do have advantages that each class can inherit some general methods used by all extended classes. It's just in my experience recently Abstract classes seem a lot harder to maintain and extend, especially with the more classes that extend the Abstract superclass.
I don't know what other people think about this but it seems to me that after starting off using Abstract classes you get a bit more savvy and then start to use Interfaces a bit more and composition. Interfaces seem to make it easier to write code that cohesive and focused on one thing, this has the result of making the code easier to extend, maintain and unit test before another class implements the interface or uses it like an instance variable
8 Comments:
I look at abstract classes as "automated interfaces" i.e. the concrete methods are an engine to make calls to the abstract methods
By Anonymous, at Thu Jul 20, 11:04:00 am 2006
I like that way of looking at Abstract classes, it might help me not abuse them like I have done in the past.
if I took the same approach to Abstract classes as I have done with interfaces then they could be just as useful. I still am not keen on the extending of the base class so much though.
I think both interfaces and abstract classes are useful in different scenarios. The most important point is that you build you code around non concrete classes.
By The Hosk, at Thu Jul 20, 12:59:00 pm 2006
Good article although I don't agree with everything: the decision of using inheritance/composition and interface/(abstract)classes should be made depending on the solution you want to reach. I like coding against interfaces because it's an abstraction of the concrete class instances you're using. But I don't implement interfaces for everything, I try to decide if it's needed or not. Interfaces don't make (abstract)classes obsolete. Sometimes you need a combination of both to reach a good solution.
To make things easier I always think of interfaces as the interface/contract and (abstract) classes as the concrete implementation.
By Anonymous, at Thu Jul 20, 01:05:00 pm 2006
Hello! Have you looked into "Traits"?
Just curious to hear your opinion.
By Anonymous, at Thu Jul 20, 02:38:00 pm 2006
Some things lend themselves to interfaces, some to abstract classes.
Composition is a clear case where interfaces are better.
Another example is in encapsulation cases within layered code, where a lower layer needs a way to reference an object from a higher layer. The lower layer can create an interface which is then implemented in the higher layer, giving the system an appropriate loose coupling. Creating a base class in the lower layer leads to unnecessary tight coupling between the layers.
I tend to limit the use of abstract classes to cases where there is a clear, tight inheritance hierarchy that requires multiple concrete implementations, but each implementation is clearly a subtype of the base. In situations like this, there are usually elements of the implementation that are shared amongst the inheritance hierarchy anyway, so using base classes provides tangible benefits.
By Tessa Norris, at Thu Jul 20, 09:01:00 pm 2006
A simple rule of thumb I follow is
Interface should be used for high level stuff i.e. integration between modules and Abstract classes should be used within a module.
By Anonymous, at Thu Jul 20, 10:07:00 pm 2006
Why people are under the impression that interfaces and abstract classes are mutually exclusive, is beyond me. In fact, this is extremely dangeours thinking and leads to lots of poor design. When you start to use both at the same time, you'll know you have moved up a notch as a developer. Until then, you still don't "get it". Good luck,
Sam
By Anonymous, at Fri Jul 21, 03:21:00 pm 2006
When I first started in Java, with my limited OO knowledge, I didn't understand how useful Interfaces are. Nowadays, when I find myself writing abstact classes, I also write an interface; with the abstract class containing much of the boiler plate code. When it comes to code reuse and refactoring you'll be glad you wrote an interface.
By Anonymous, at Fri Jul 21, 03:41:00 pm 2006
Post a Comment
<< Home