简体   繁体   中英

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

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?

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.

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

本网站上发布的关于抽象类和接口的答案将帮助您了解设计差异。

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.

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM