7

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.

Emil
  • 13,577
  • 18
  • 69
  • 108
Ebbu Abraham
  • 2,036
  • 5
  • 22
  • 30
  • 1
    Read this: http://stackoverflow.com/questions/5953851/interface-vs-abstract-classes Or this: http://stackoverflow.com/questions/1221512/abstract-class-and-interface-class Or any of the other hundred questions identical to this one – RonK Jun 23 '11 at 13:13

6 Answers6

5

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • yes I understand that I can have both an abstract class and interface, but is that the best way ?? – Ebbu Abraham Jun 23 '11 at 13:17
  • http://en.wikipedia.org/wiki/Template_method_pattern Here they used only Abstract class Game instead of using abstract class and interface. Any idea why it is done like that. Any advantage ?? – Ebbu Abraham Jun 23 '11 at 13:26
5

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.

roberttdev
  • 42,762
  • 2
  • 20
  • 23
3

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.

deadalnix
  • 2,255
  • 18
  • 24
  • yes it makes sense. But is there any disadvantage in the design if I add an interface say GameInterface and do all the method declarations there and let the abstract class Game implement GameInterface. This question is just out of curiosity. Thanks – Ebbu Abraham Jun 23 '11 at 13:24
  • This is effectively a common practice. You'll see that kind of stuff a lot in java's API for exemple. But in thoses case, pattern based on composition (decorator for exemple) are often a better solution. – deadalnix Jun 23 '11 at 13:37
1

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?

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
0

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.

Doug Clutter
  • 3,646
  • 2
  • 29
  • 31
0

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.

nsfyn55
  • 14,875
  • 8
  • 50
  • 77