Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class hierarchy design - interfaces + base class vs. abstract class

Tags:

c#

oop

Sometimes I wonder that we do have interfaces and abstract classes for two different reasons:

  1. If you just want multiple implementations for design purposes and something to code against at the development time then interface is the best choice that we have got.

  2. If you want to reuse the code then we might go for the abstract classes.

BUT, I have seen something that is neither.

In some designs, it is like this Interface > BaseClass > Child class.

So basically a base class implements all the methods and then the child class inherits the definition from it. Why can we not use an abstract class instead of this setup? Or this is a flawed setup?

like image 802
Lost Avatar asked Nov 29 '22 03:11

Lost


1 Answers

The most simple reasining is if an object has a IS-A relation then use an (abstract) base class. Like a horse IS an animal.

If there is a CAN relation, then think about interfaces, like if a Duck CAN fly, use IFlying as interface for a duck that can fly.

like image 105
Michel Keijzers Avatar answered Dec 05 '22 03:12

Michel Keijzers