DEV Community

Cover image for ABSTRACT CLASS VS INTERFACE
Common Khadka
Common Khadka

Posted on

ABSTRACT CLASS VS INTERFACE

One of the most frequently asked questions in any technical interview is "Tell me something about abstract class and interface."
Although it is rather straightforward, occasionally it can be difficult to distinguish between them.

Let's begin with their description:

Abstract Class
A class that cannot be created on its own but is frequently used as a model for other classes is referred to as an abstract class. It
may include both concrete (implemented) and abstract (not yet
implemented) methods.

Interface
An interface refers to a contract that specifies a group of methods that a class which implements it, must offer. It does not include any implementation; it simply contains method signatures.

The most important thing to take away from this is that although Interface only has unimplemented methods, Abstract classes can contain both implemented and unimplemented methods.

Usage

Abstract Class
Use an abstract class when you have a base class with shared behavior between its subclasses. Some methods should have a standard implementation, while others should be left up to subclasses to implement.

Interface
Use an interface when you wish to establish a contract that several classes can operate without imposing a particular class hierarchy. Method signatures may be inherited more than once.

Implementing a method:

Abstract Class
Both abstract and concrete methods are possible for an abstract class.
The abstract methods must have implementations provided by subclasses.

Interface:
Only method signatures are present; no implementation is
present. All of the methods in an interface must have concrete
implementations in classes that implement the interface.

I have an simple real life example which will make it more clear.

Abstract Class
Abstract class are more like a company employee's. Company can have full time employee who will handle day to day work. And, company can also hire part time employee who will handle that specific work.

Interface
Interface is like a job contractor. You just define job to be done to a contractor and they will handle the rest. if any employee leaves the work than it won't matter to you because it will be handled by the contractor itself.

Let's explain with the help of some code examples.


using System;
// Abstract class  
abstract  class  Shape
{ 
// Concrete method  
    public void PrintColor() 
    { 
        Console.WriteLine("The shape is colored."); 
    } 
    // Abstract method to calculate area (to be implemented by subclasses)  
    public abstract double CalculateArea(); 
} 
// Concrete subclass  
class  Circle : Shape 
{ 
    private  double radius; 
    public Circle(double radius) 
    { 
        this.radius = radius; 
    } 
    public override double CalculateArea() 
    { 
        return Math.PI * radius * radius; 
    } 
} 
// Concrete subclass 
class  Rectangle : Shape 
{ 
    private  double length;
    private  double width; 
    public Rectangle(double length, double width) 
    { 
        this.length = length; this.width = width; 
    } 
    public override double CalculateArea() 
    { 
        return length * width; 
    } 
}

Enter fullscreen mode Exit fullscreen mode

In this C# example, the Shape class is an abstract class with a concrete method PrintColor() and an abstract method CalculateArea(). Subclasses Circle and Rectangle inherit from Shape and provide concrete implementations for the CalculateArea() method.

using System;

// Interface for drawing
interface IDrawable
{
    void Draw();
}

// Interface for filling
interface IFillable
{
    void Fill();
}

// Class implementing both interfaces
class Circle : IDrawable, IFillable
{
    private double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public void Draw()
    {
        // Implementation for drawing a circle
        Console.WriteLine("Drawing a circle.");
    }

    public void Fill()
    {
        // Implementation for filling a circle
        Console.WriteLine("Filling a circle.");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this C# example, we have two interfaces, IDrawable and IFillable, each with a method signature. The Circle class implements both interfaces and provides concrete implementations for the Draw() and Fill() methods. This demonstrates how C# allows a class to implement multiple interfaces to achieve multiple inheritance of method signatures.

I guess that's all. Hope you understand. Enjoy.

Top comments (0)