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.
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 |
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.
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
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.
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
What is an interface in C#?
Answer: An interface defines a contract. Classes implementing it provide the required members and behaviour.
What is an abstract class?
Answer: It is a class that cannot be instantiated directly. It can contain implemented methods, fields and abstract members.
Can a class implement multiple interfaces?
Answer: Yes. A class can implement multiple interfaces, allowing it to support different contracts.
Can an abstract class have a constructor?
Answer: Yes. Its constructor runs when a derived class object is created and can initialize shared state.
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.