4

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lost
  • 12,007
  • 32
  • 121
  • 193

8 Answers8

11

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.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • 2
    This is a great succinct easy to remember answer – Matt Wilko Dec 03 '14 at 10:58
  • Wow! First time I'm seeing this example of 'Is' .vs 'can'. Makes sense, especially in a simulated taxonomy (or coded abstraction of the real world). Can you provide a good link for further reading? Thanks! – Vance McCorkle Mar 16 '22 at 17:55
  • 1
    @VanceMcCorkle https://www.w3resource.com/java-tutorial/inheritance-composition-relationship.php, https://www.baeldung.com/java-inheritance-composition – Michel Keijzers Mar 16 '22 at 18:54
3

Interfaces can be used to implement multiple interfaces into one class. You can't derive from multiple classes in C#.

BlueM
  • 6,523
  • 1
  • 25
  • 30
3

This answers posted on this site about Abstract classes and interfaces will help you understand the design differences.

Community
  • 1
  • 1
Ebad Masood
  • 2,389
  • 28
  • 46
3

It's a matter of design and personal taste, usually when you declare a interface you don't really care about the specific implementation but you just care about the wrapped functionality that the interface should provide.

Having an abstract class does define some specified behavior, this is something that you don't want always.

At the same time having abstract classes would forbid you from inheriting more than one, so this would limit the functionality it can have.

When designing a class hierarchy you question yourself "what should object used in this situation be able to do?" and define an interface for that functionality, then, while providing implementation, you can realize that some of that functionality should be grouped and reused for some child classes but it is something that comes later.

Jack
  • 131,802
  • 30
  • 241
  • 343
2

It is due to multiple inheritance of classes is prohibited in C#. But it is allowed to implement multiple interfaces.

Sergey Savenko
  • 666
  • 6
  • 11
2

An interface is essentially a contract of a piece of functionality; specifying certain methods and properties. Sections of code can be written against this interface without any knowledge of the implementation detail.

An abstract class may implement some interface(s), but it may also contain some common functionality that all derived types may need. It may also include abstract or virtual methods/properties that allow derived types to customize some of the functionality.

Consider a scenario where you construct some object model, that you code against, using interfaces to define the structure of said model, but you want to allow consumers of that code to potentially write their own implementations if they so wish, without affecting the core design. You may then build your own implementation with some base class for one implementation. Derived types would then inherit this base class, which in turn implements the interfaces of your object model. Someone else may then come along and write their own implementation, but want some different functionality or behaviour to your existing implementation. As such, they write their own base class with their common functionality, and extend it with their own derived types.

Following this scenario, there is no real reason why the use of both an abstract class and and interface should not be used. It all depends on your purpose and how you are constructing your code.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
2

An interface defines a set of attributes and functionality that a class must implement and can be implimented by any number of classes. There is no reason why a base class should not implement an interface. For example many different base classes might use IComparable.

Void
  • 265
  • 1
  • 3
  • 11
0

Main rules for an interface are

  1. All methods should be abstract
  2. No implementation
  3. Interface can extend any number of Interfaces
  4. All fields should be public, static and final
  5. All methods should be overridden in the inherited class
bizimunda
  • 819
  • 2
  • 9
  • 26
  • As this is a c# thread all of that is enforced by the IDE when you declare that something is an interface. – Void May 31 '16 at 10:37