Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you decide between using an Abstract Class and an Interface? [duplicate]

I have been getting deeper into the world of OOP, design patterns, and actionscript 3 and I am still curious how to know when to use an Abstract class (pseudo for AS3 which doesn't support Abstract classes) and an interface. To me both just serve as templates that make sure certain methods are implemented in a given class. Is the difference solely in the fact that Abstract classes require inheritance and an Interface merely extends?

like image 565
Brian Hodge Avatar asked Feb 17 '09 18:02

Brian Hodge


People also ask

How do you choose an interface or abstract class?

In general, you should choose interfaces over abstract classes. The use of an interface separates your design from any implementation details. Even if you declare a purely abstract class without any method implementations, you must inherit from it to define classes that share the behavior defined by its methods.

When should I use abstract class and interface?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.

How do you know whether to make a class A subclass an abstract class or an interface?

Suppose if a class has a method that is abstract, then the class itself must be abstract. Abstract classes have no restrictions on field and method modifiers, while in an interface, all are public by default.

Which is better abstract or interface?

An interface is better than a abstract class when you want multiple classes to implement that interface and when you don't have to inherit default behavior. Show activity on this post. In order to provide WebServices or do JMock tests you dont need the actual implementation, you just need an interface definition.


2 Answers

Use an abstract class if you have some functionality that you want it's subclasses to have. For instance, if you have a set of functions that you want all of the base abstract class's subclasses to have.

Use an interface if you just want a general contract on behavior/functionality. If you have a function or object that you want to take in a set of different objects, use an interface. Then you can change out the object that is passed in, without changing the method or object that is taking it.

Interfaces are typically loose, compared to Abstract classes. You wouldn't want to use interfaces in a situation where you are constantly writing the same code for all of the interface's methods. Use an abstract class and define each method once.

Also, if you are trying to create a specific object inheritance hierarchy, you really wouldn't want to try to do that with just interfaces.

Also, again, in some languages you can only have a single base class, and if an object already has a base class, you are going to have to do some refactoring in order to use an abstract base class. This may or may not mean that you might want to use an inteface instead.

As @tvanfosson notes, it's not a bad idea to use a lot of interfaces, when you really understand abstract classes and interfaces, it's not really an either/or situation. A particular situation could use both abstract classes and interfaces or neither. I like to use interfaces sometimes simply to restrict what a method or object can access on a passed in parameter object.

like image 132
Mark Rogers Avatar answered Sep 24 '22 03:09

Mark Rogers


Abstract classes offer the possibility to implement specific methods and require others to be implemented in the inheriting class. With interfaces, everything has to be implemented in the implementing class.

like image 29
raimue Avatar answered Sep 25 '22 03:09

raimue