Interface and Abstract Class

interface and abstract class in c sharp

What is interface and abstract class?

Interface and abstract class are two of the most basic constructs of OOPS. Their goal is to achieve abstraction. That means, we can maintain object hierarchy using these constructs. They are similar in the way that they enforce their subclasses to implement the unimplemented methods. Hence making classes more meaningful. Despite this similarity, there are certain differences between them.

Before jumping into the topic, let’s discuss relationships between the objects.

  1. Is-A relationship, also called parent-child relationship.
  2. Has-A relationship.

Both interface and abstract class have “Is-A” relationship with their implementations/subclasses. That means Royal Enfield is a class that implements an interface (or inherit abstract class or even class) Motor Cycle. Then we can say Royal Enfield “Is-A” motorcycle.

Interface:

The interface defines the contract. It defines an agreement between the interface and implementing a class. The subclass has to implement everything declared inside an interface.

Consider an interface, IMotorCycle, and a class, RoyalEnfield. IMotorCycle has declared a few methods like DesignBrandLogo, AddEngine, MountSeat etc. For RoyalEnfield to become IMotorCycle, it has to implement all three methods. Otherwise, it will fail the contract. That means a concrete class MUST implement all the members of the interface and that’s the contract.

interface IMotorCycle 
{
    void DesignBrandLogo();
    void AddEngine();
    void MountSeat();
}

class RoyalEnfield : IMotorCycle 
{
    public void DesignBrandLogo()
    {
        //some logic
    }

    public void AddEngine()
    {
        //some logic
    }

    public void MountSeat()
    {
        //some logic
    }
}

The interface is one of the most useful OOP concepts. Many design principles and design patterns are incomplete without it.

Abstract Class:

Unlike interface, an abstract class can have implementation for some of their methods. It is important when you want to provide the default implementation for some part of the class and leave other functionality for subclasses. Consider the same example as above. In this example, we have implemented AddEngine and MountSeat. In reality, RoyalEnfield can’t implement them. This is because RoyalEnfield doesn’t know the implementation details for them. For example, CC, power, and the torque of the engine. Similarly, for MountSeat it doesn’t know whether the seat will be single or split. In such situations, it would be better to create an abstract class for that. So, in the below example, this class will implement DesignBrandLogo but leave others for child classes to implement.

public abstract class RoyalEnfield
{
    public void DesignBrandLogo()
    {
        //some logic
    }

    public abstract void AddEngine();

    public abstract void MountSeat();
}

Now, subclass like Classic350 will implement the rest of the methods.

public abstract class Classic350 : RoyalEnfield
{
    //No need to implement DesignBrandLogo as it will be inherited from parent class

    public override void AddEngine()
    {
        //some logic
    }

    public override void MountSeat()
    {
        //some logic
    }
}

Syntax:

The interface is declared by using the “interface” keyword. Interface members are always public so they do not accept explicit access modifiers.

An abstract class is defined by using the “abstract” keyword. We need to use the same keyword for defining un-implemented methods. Subclass uses the “override” keyword for implementing them.

When to use them:

Every time you create a non-static class, use the interface. Even though they are not important at that time but can be used if there is a need to create a similar class in the future. They also make it easy to mock the classes for setting up Unit Tests.

When you are in a position to provide implementation to some functionalities but not all, use an abstract class.

Differences:

InterfaceAbstract Class
The concrete class must implement all the members on the interface.An abstract class is a partially implemented class. Which means it can have both, implemented and unimplemented methods.
Interface support multiple inheritance.Like normal classes, the abstract class also does not support multiple inheritance.
Interface can’t have an implementation. So, it can not have variables either as they are used in the implementation.There can be implemented methods so they can have variables.
Interfaces can inherit other interfaces but not classes.Like normal classes, these can also inherit interfaces as well as a single class.
All members are by default public.Abstract classes can have all kinds of access modifiers.

To see the interface in action, you can visit SOLID Principles.


Few important questions:

Why classes doesn’t support multiple inheritance?

Two reasons that I can think of,

  1. Consider two classes, Television and Car, both of them have a method Start. Now if some classes inherits both and call Start, which Start will exactly be invoked? This would have been very difficult for the compiler decide.
  2. Microsoft made it clear that it would have been very complex feature to implement while getting very little benefit. If you’re curious, just head over to that discussion by Microsoft itself.

Why interfaces support multiple inheritance?

Because they are not implemented, so can’t be directly called.


Can abstract classes be instantiated?

No, like interface, they also can’t be instantiated. Because they can have unimplemented methods and we can’t call unimplemented methods.


If abstract class can not be instantiates then why they can have constructor?

It is true that we can not instantiate abstract classes but they can be instantiated through subclasses. This is because they can call its implemented methods. Hence, they can have constructors too.


How to implement two interfaces that have same method?

You can have only one method with the same signature in a class. If both interfaces have the same method, implementing one would be enough.

interface I1
{
	void M();
}

interface I2
{
	void M();
}

class C : I1, I2
{
    public void M()
    {
    }
}

Interfaces are a lot more powerful than what I described. They are the backbone of software architecture. I have explored many design patterns and would love to share them with you. Stay tuned and happy coding.

Summary:

The interface allows you to define a contract between itself and implementing classes to enforce abstraction. The abstract class enables developers to write partially implemented classes. This means you can write generalized logic by yourself and enable other developers to write more specific logic while respecting abstraction. These two constructs may not be mandatory for implementing some basic applications but are very useful when writing scalable and testable code.

Leave a Reply

Your email address will not be published. Required fields are marked *

Coding Crest Back To The Top