1

I have some doubts when to use abstract class and if I need to always code interface. An example:

I have will have series of custom entities, and all of them need to implement SomeMethod() and most of them need to implement AnotherMethod() method.

  • SomeMethod() will be entity specific, each entity will have different code.

  • AnotherMethod() is implemented by most, but not all, and the code is the same for all.

How is this modeled? My idea is that each new entity must implement SomeMethod() and is able to use AnotherMethod().

Thanks, Goran

Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
Goran
  • 77
  • 1
  • 8

4 Answers4

3

AnotherMethod should likely be implemented in an abstract class so you don't repeat the code all over the place.

If SomeMethod is related functionaloty, it could be left in the same abstract class without an implementation, forcing children to implement it. If the functionality is not related to AnotherMethod, you could put it in its own interface.

Erix
  • 7,059
  • 2
  • 35
  • 61
  • So, if for example, I have some common functinality, that will be inherited by all deriving classes, and I have a few methods which will be common to most, but not all of derived clases, then It is practice to implement them, and override them in those classes where I need different functinality? – Goran Mar 03 '11 at 11:29
1

You're right, for SomeMethod(), using abstract parent class with abstract method is a good idea. You can also use interfaces, depending on the meaning of the method. For example, if different classes represent different animals and the method is Move(Coordinate destination), an abstract parent class is better. If, on the other hand, different classes have nothing in common and the method is SerializeToJSON(), you should better use interfaces.

If AnotherMethod() is implemented by some of the classes, again, you can use an abstract parent class (with a non-abstract protected/public method). Of course, don't inherit from this parent the classes which do not have to implement AnotherMethod().

Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
  • So, if for example, I have some common functinality, that will be inherited by all deriving classes, and I have a few methods which will be common to most, but not all of derived clases, then It is practice to implement them, and override them in those classes where I need different functinality? – Goran Mar 03 '11 at 11:30
0

A big difference between interfaces and abstract classes is that the abstract class can have some implementation where the interface is strictly a contract and data type. In the examples you give, you could use an interface to require the implementation of both SomeMethod and AnotherMethod but you wouldn't be able to write any code for AnotherMethod since the interface would just have a method signature.

In an abstract class you could define SomeMethod as abstract and therefore require an implementation from classes which inherit from it but you could also create the implementation of AnotherMethod and have a single implmentation since you say that it will be the same for a lot of your classes.

t3rse
  • 10,024
  • 11
  • 57
  • 84
  • Hi David, I know the definitions of abstract class and intefrace, my question was more related to weather it is good idea to implement several moethods in base class, which would be overriden by some derived classes, or there is some pattern to make this more clean, – Goran Mar 03 '11 at 11:25
0

A good situation to think of replacing inheritance with aggregation.

I'd extract AnotherMethod() to other class, say, AnotherMethodRunner, and add a getAnotherMethodRunner() to a base interface. If AnotherMethod() is a property of derived class, it will have one, if not - it will return null or Null Object.

I personally usually take a nonempty abstract base class as a call to more precise interface extraction.

Community
  • 1
  • 1
Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91