[Solar-talk] [OT] interfaces or abstract classes

Andrew Shell andrew at andrewshell.org
Wed Sep 20 09:02:56 PDT 2006


The biggest advantage I see to using interfaces is that you can 
implement as many interfaces as you want when creating a class.

You typically define an interface when you are planning on using an 
object as a dependency in another class.  By defining the interface you 
are specifying the api that you will be using by the code and so as long 
as your class implements the interface you can verify it in the client 
code by using the instanceof operator and then you are safe to use the 
methods you are expecting.

An abstract class on the other hand is useful if you have a whole bunch 
of functions that are going to be identical across subclasses except for 
a few methods that are unique for each subclass.  This way you can 
extend the abstract class like normal and then just impement the 
abstract methods to complete the class.  This limits you a bit.  You can 
only extend one class.  So this makes sense if you are constructing a 
clear hierarchy. Otherwise you may be better off separating the changing 
logic into other classes (that implement a common interface) and pass 
them into the main class as a dependency.

For more information I did a quick search on Google and came up with 
this url...
http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html
It's for Java but I think the ideas translate.

Let me know if you have any other questions...

Andrew Shell

Stuardo -StR- Rodríguez wrote:
> Sorry to ask here this but I have no other place to ask
>
> My question is...  when should I use interfaces and when to use abstract 
> classes.
>
> I do understand that interfaces are just a list of methods some concrete class 
> should implement and nothing more
>
> Abstract class are like a interfaces but they can tell wich methods are 
> abstract and wich are already defined.. so that is a + for using abstract 
> classes
>
> But a concrete class can implement not just one interface but can only extend 
> from one single abstract class so that is a + for using interfaces
>
> But that is all I can see...   so after that, I do not get when to use 
> interfaces and when to use abstrac classes.   So, if we could inherit from 
> many abstrac classes, we would never need interfaces, right?
> **
> Can anyone help me to understand this?
>
>
>   



More information about the solar-talk mailing list