(Regular Class + interface) vs Abstract class

I recently encountered a question on abstract class.

Functionality of Abstract classes can be achieved by using combination of (Regular class with Protected Constructor + an interface).

What is the benefit of using Abstract Class over (Regular class with protected constructor + interface).

IMHO, Purpose of Abstract class to have common feature that needs to be available across the class hierarchy. It can pose restriction on sub-classes to implement certain features by Abstract methods. It can allow Sub-Classes to override the common behavior.

Abstract Class doesn't serve a purpose of as concrete object. So, It doesn't allow to instantiate the abstract class.

However,We can achieve same thing using Regular Class + interface.

  1. Mark Regular Class constructor as protected, So object can't be created alone
  2. provide default implementation of common features and mark them virtual in case if they need to be overridden by sub class.
  3. Use interface to force sub-classes to implement certain features.

So, Is there any extra feature which Abstract class offer?

I could not think of any other. Interviewers was trying to know what other benefits Abstract class have over Regular Class with protected constructor + interface.

3 answers

  • answered 2018-01-14 11:18 Zohar Peled

    One thing that I can think of is that by using an abstract class you can force a specific implementation simply by not marking a method or property as virtual, while using an interface you can't prevent classes from implementing the interface but not derive from your base class.

    Another benefit of using an abstract class is that you can simply add functionality to your abstract class without having to worry about having all your derived classes implementations - again, since you can't prevent a class from implementing an interface without deriving from your base class.

    Also, an abstract class can have protected fields, methods, events etc', but an interface can't.

    It all boils down to the fact that you can't force classes that implement your interface to derive from your "regular" base class.

  • answered 2018-01-14 11:19 Ian Robertson

    The main benefit of the abstract class is to force the developer to create a subclass that inherits from the abstract class in order to use base/shared functionality and fields.

    You cannot directly new-up an abstract class. You can new-up a regular class + interface, and you are not forced to inherit or override anything in the base.

    With an abstract class, you can reduce the number of files - i.e. no interfaces, but most folks would probably like to keep those for registration with an IoC container and dependency injection.

  • answered 2018-01-14 13:00 Gholamali-Irani

    First of all, there is many questions and answers about differences between Abstract Class and Interfaces like: this. There are a lot of remarkable answers. But most of them are about programming and syntax.

    I want to look from Design Perspective:

    I think that Abstract Class can not play the Role of Interface (+ Regular Class) in Software Design.

    Abstract Class:
    The main goal of Abstract Class is Abstraction Principle. To overcome this complexity, Abstract classes are used to make Hierarchies in similar looking classes. All classes in the hierarchy are extending base classes functionalities and extending types of base classes.

    However, Interfaces are used for Interactions between classes. These classes can be similar or not. They can be from different hierarchies and different types.

    Also, they are huge difference between inheriting from a class (even Abstract class) and implementing an interface. Interfaces are not TYPES. They are shared boundary across which two or more separate components of a computer system exchange information.