开发者

Is there a scenario where an Abstract class is preferred over Interface

开发者 https://www.devze.com 2023-03-15 09:27 出处:网络
I am having a scenario where there are several subclasses which have got similar implementations and some extra methods for which the implementations differ with each subclass. I assume that an abstra

I am having a scenario where there are several subclasses which have got similar implementations and some extra methods for which the implementations differ with each subclass. I assume that an abstract class would be a good choice for this scenario. But would it be better if that abstract class implements an interface which contains all method declarations.Or should I just stick with the abstract class instead.

开发者_如何学运维

In short, I would like to know the scenarios where I should prefer Abstract classes at the top of the hierarchy rather than an Interface.


Use the abstract class if your subclasses have is-a relationship with the abstract class.

You can have both an abstract class and an interface - the abstract class specifying implementations, and the interface specifying the API.

The collections frameworks is an example of that - it has ArrayList extends AbstractList implements List


An abstract class doesn't have to be completely abstract. You can define certain functions (and variables) that all subclasses will use as-is, and leave only certain methods to be implemented by the subclasses. An interface has the limitation that no functions can be defined.

On the flip side, interfaces allow the flexibility for a class to implement multiple interfaces, whereas a class can only extend one other class. In this sense, an interface will probably always be preferable to a purely abstract class. But there are still plenty of uses for abstract classes which do contain some reused functionality.


Abstract class can provide default behaviour where Interfaces cannot. This make sens when a part of the behaviour will be common accross several subclasses.

A very good use of that is the template method pattern : http://en.wikipedia.org/wiki/Template_method_pattern which reduce sequential coupling.


Remember that with Abstract class, you can define data that the subclasses have. With interface, you can only define methods that implementers must implement. So in your situation, do you need common data and common methods or just common methods?


Abstract classes let you carry around some code/data that you can then use in the inherited classes. They are great for that, but use inheritance very sparingly. Only inherit from a class if the new class is absolutely interchangeable with the abstract.

Interfaces contain no code.

I prefer to code to interfaces whenever possible. I also like to keep those interfaces as small as possible. This leaves me the flexibility to swap out the underlying impementation at a later time.

If you code to an abstract class, it is harder to swap out the implementation at a later time.

You can apply an interface (or several small interfaces) to the abstract class. Sounds like this may be your best approach.


Always favor interface. You should try very hard to avoid abstract classes. With that being said abstract classes have a place especially in libraryish code akin to java collections. In these places you are building classes that are expressly designed for the purpose of being extended and there is a lot of value in consolidating behavior especially from a quality perspective.

0

精彩评论

暂无评论...
验证码 换一张
取 消