Skip to Content
All posts

Demystifying Interfaces and Abstract Classes in C#

2 min read ·  — #csharp-interview#junior#oop

Demystifying Interfaces and Abstract Classes in C#

As a software engineer, a solid understanding of key object-oriented programming concepts is indispensable, particularly when preparing for technical interviews. Among these crucial concepts are Interfaces and Abstract Classes, two foundational elements of C#. While they might seem confusing or interchangeable at first glance, they each serve unique purposes in software design, impacting code reusability and modularity.

In this post, we'll delve deep into the world of Interfaces and Abstract Classes, unraveling their similarities, differences, and appropriate use cases. We'll learn what they are, look at some examples of how they're used in C#, and analyze their key features. Whether you're a seasoned programmer wanting to refresh your knowledge or a newbie preparing for your upcoming technical interview, this comprehensive guide is designed to give you a thorough understanding and boost your confidence.

Let's start with understanding what they are.

Interfaces

An interface is essentially a contract that defines a group of related functionalities. An interface doesn't provide implementation for any of these functionalities, it only declares them. All the methods declared in an interface are implicitly public and abstract. Any class or struct that implements the interface must provide the implementation for all of its members.

Example:

public interface IAnimal
{
    void Speak();
}

Here is a class implementing the interface:

public class Dog : IAnimal
{
    public void Speak()
    {
        Console.WriteLine("Woof!");
    }
}

Abstract Classes

An abstract class, on the other hand, provides a common definition of a base class that multiple derived classes can share. It can have both abstract methods (methods without a body) and regular methods. Abstract classes cannot be instantiated. They must be inherited and their abstract methods must be implemented by the child class.

Example:

public abstract class Animal
{
    public abstract void Speak();

    public void Sleep()
    {
        Console.WriteLine("Zzz...");
    }
}

Here is a class that extends the abstract class:

public class Cat : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Meow!");
    }
}

Now, let's discuss some key differences:

Implementation: Interfaces cannot provide any code, just the signature. But, abstract classes can provide some method implementation.

Multiple inheritances: A class can implement several interfaces, while a class can only inherit from one abstract class.

Default Implementation: Interfaces cannot have fields or constructor, while an abstract class can have fields and constructors. Hence, you can have a default implementation in an abstract class.

Access Modifiers: In an interface, all methods are implicitly public. But, in an abstract class, you can have a method with access modifiers like private, protected, internal, protected internal, or public.

State or Fields: If you want to maintain a state, go for an abstract class. An interface cannot have fields.

I hope this post has helped you to understand the difference between interfaces and abstract classes in C#. Remember, when to use which largely depends on the design requirement. Good luck with your interview preparation!

References