LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Interfaces and Abstract Classes in C#

Both help us define common behaviour for different classes. An interface defines a contract that a class must follow. An abstract class can provide both common code and methods that child classes must implement. These concepts are widely used in enterprise applications, payment systems, dependency injection and API development.

1Interfaces and Abstract Classes

Hi friends, in this article we will learn interfaces and abstract classes in C#. Both help us define common behaviour for different classes. An interface defines a contract that a class must follow. An abstract class can provide both common code and methods that child classes must implement. These concepts are widely used in enterprise applications, payment systems, dependency injection and API development.

2Think About Online Payments

Imagine an application supporting UPI, credit cards and net banking. Every payment method needs to process a payment, but each method follows a different implementation.

We can define a common interface called IPayment. UPI and CardPayment implement that contract. The application can call ProcessPayment without knowing the internal payment logic.

Remember An interface defines what a class must do. An abstract class can also define how some shared behaviour works.

3Understand the Differences

Interface

An interface defines a contract. A class uses a colon to implement it and provides the required members.

Abstract Class

An abstract class cannot be instantiated directly. It can contain fields, constructors, implemented methods and abstract methods.

Multiple Interfaces

A C# class can inherit one class but implement multiple interfaces. This supports flexible designs.

When Should You Use Each?

Interface Abstract Class
Defines a contract Shares common code
Multiple interfaces allowed Only one base class
No instance fields Can contain instance fields
Use for capabilities Use for shared base behaviour
Pro Tip Use interfaces when unrelated classes need to support the same capability.
Common Mistake Do not choose an abstract class just because two classes have similar method names. Check whether they share a meaningful base relationship.

4Create a Payment Contract

Let's define IPayment and implement it in two payment classes. The PaymentService depends on the interface, not a specific payment class.

Program.csC#
using System;

interface IPayment
{
    void ProcessPayment();
}

abstract class Payment
{
    public void ShowMessage()
    {
        Console.WriteLine("Payment started");
    }
}

class UpiPayment : Payment, IPayment
{
    public void ProcessPayment()
    {
        Console.WriteLine("UPI payment completed");
    }
}

class CardPayment : Payment, IPayment
{
    public void ProcessPayment()
    {
        Console.WriteLine("Card payment completed");
    }
}

class Program
{
    static void Main()
    {
        IPayment payment = new UpiPayment();
        ((Payment)payment).ShowMessage();
        payment.ProcessPayment();

        payment = new CardPayment();
        ((Payment)payment).ShowMessage();
        payment.ProcessPayment();
    }
}

Both classes implement IPayment and inherit the shared ShowMessage method from Payment. The interface reference can point to either payment object.

5Check the Payment Results

Output
Payment started
UPI payment completed
Payment started
Card payment completed

Both payment classes use the same inherited message, but each provides its own payment implementation.

6Watch Both Payments Execute

Follow the interface reference as it points first to UPI and then to card payment. The shared method comes from the abstract base class.

Program.cs - Visual Studio Code
▤⌕⑂▦
● Program.cs
 
PROBLEMS  OUTPUT  DEBUG CONSOLE  TERMINAL
 
ReadyUTF-8  CRLF  C#

7Choose the Right Design

Interfaces are useful for defining contracts across different classes. Abstract classes are useful when related classes share common state or implementation. Both improve flexibility and code reuse.

  • Use interfaces to define capabilities.
  • Use abstract classes for shared base behaviour.
  • Depend on abstractions instead of concrete classes.

8Questions Interviewers May Ask

Q1Easy

What is an interface in C#?

Answer: An interface defines a contract. Classes implementing it provide the required members and behaviour.

Q2Easy

What is an abstract class?

Answer: It is a class that cannot be instantiated directly. It can contain implemented methods, fields and abstract members.

Q3Medium

Can a class implement multiple interfaces?

Answer: Yes. A class can implement multiple interfaces, allowing it to support different contracts.

Q4Medium

Can an abstract class have a constructor?

Answer: Yes. Its constructor runs when a derived class object is created and can initialize shared state.

Q5Hard

Can an interface contain implemented methods?

Answer: Yes. Modern C# supports default interface implementations. A class can use the default implementation or provide its own.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.