Difference between an abstract class and an interface

Difference between an abstract class and an interface

Learn the difference between an abstract class and an interface in C#

ยท

3 min read

Introduction

In object-oriented programming, abstraction consists in hiding the details of the implementation of a function or a class and showing only the essential information to the user. Abstraction can be achieved by using abstract classes or interfaces. Abstract classes and interfaces are widely used in software development and programming, especially when dealing with object-oriented programming problems. In this article, we will try to understand what exactly an abstract class is, what an interface is, and how and when to use them. The programming language used in this article is C#.

Abstract Classes

What are abstract classes?

The abstract modifier indicates that a function or a class is not complete and has missing components and needs to be implemented, when we use the abstract modifier with classes, we indicate that the class is intended to be a base class for other classes not instantiated on its own. Members marked as abstract must be implemented by non-abstract classes that derive from the abstract class.

Code Example of abstract classes

Let's take a simple example, let's create a base class called Human that has the abstract method Walk, then we can create the class man that implements the walk method.

using System;
namespace Demo
{  
    abstract class Human
    {
        public abstract void Walk();
    }
    class Man : Human
    {
        public override void Walk()
        {
            Console.WriteLine("Man is walking ...");
        }
    }


    internal class Program
    {
        static void Main(string[] args)
        {
            Man man1 = new Man();
            man1.Walk();
            Console.ReadLine();
        }
    }
}

Other examples where the abstract class can be used : create a vehicle class then create other classes like car, bike, truck... that implements the functions declared in the vehicle class.

Features of abstract classes

Abstract methods have the following features:

  • An abstract method is implicitly a virtual method.

  • Abstract method declarations are only permitted in abstract classes.

  • Because an abstract method declaration provides no actual implementation, there is no method body;

Interfaces

Interfaces are blueprint of a class. It cannot have method body and cannot be instantiated. Interfaces are used to achieve multiple inheritance that cannot be achieved by a class. It is used to achieve fully abstraction because it cannot have method body. It is used to achieve full abstraction because methods in an interface cannot have a body.

Let's take a simple example :

using System;  
public interface Drawable  
{  
    void draw();  
}  
public class Rectangle : Drawable  
{  
    public void draw()  
    {  
        Console.WriteLine("drawing rectangle...");  
    }  
}  
public class Circle : Drawable  
{  
    public void draw()  
    {  
        Console.WriteLine("drawing circle...");  
    }  
}

Conclusion

Unlike abstract classes that can have a method body, interfaces are used to achieve full abstraction because methods in an interface cannot have a body.

Did you find this article valuable?

Support Interrupt101 by becoming a sponsor. Any amount is appreciated!

ย