Abstarct and Interface
In C#, an abstract class is a class that cannot be instantiated directly and is
meant to be inherited by other classes.
It can contain abstract members (without implementation) and non-abstract
members (with implementation).
Key Points about Abstract Classes in C#:
Declared using the abstract keyword.
Can have abstract methods (no body, must be overridden in derived
class).
Can also have normal methods, fields, properties, and constructors.
A class inheriting an abstract class must provide implementations for all
abstract members.
Useful when you want to define a base template but leave certain details
for subclasses to implement.
using System;
abstract class Shape
{
// Abstract method (no implementation here)
public abstract double GetArea();
// Non-abstract method (common functionality)
public void Display()
{
Console.WriteLine("This is a shape.");
}
}
class Circle : Shape
{
private double radius;
public Circle(double r)
{
radius = r;
}
// Implementing abstract method
public override double GetArea()
{
return Math.PI * radius * radius;
}
}
class Program
{
static void Main()
{
Shape shape1 = new Circle(5);
shape1.Display();
Console.WriteLine($"Circle Area: {shape1.GetArea()}");
}
}
✅ Why use abstract classes?
When you want to define a common base but ensure derived classes
implement certain functionalities.
Perfect for template-style designs where part of the implementation is
shared and part is specific to the subclass.
Abstract classes in C# are used mainly for code reusability + enforcing a
contract while still allowing flexibility for child classes to define their own
behavior.
Why Abstract Classes Are Used
1️⃣ To define a common template for related classes
You can put shared logic in the abstract class and force subclasses to
implement specific methods.
Example: Shape defines a GetArea() method but doesn’t know how to
calculate it—Circle and Rectangle know their own formulas.
2️⃣ To enforce a contract without full implementation
Abstract methods must be overridden in derived classes.
This ensures that every subclass will have certain functionalities.
3️⃣ To achieve partial abstraction
Unlike an interface (100% abstraction), an abstract class can have:
o Fully defined methods (common behavior)
o Abstract methods (must be implemented by child)
This is useful when you want to provide some ready-made
functionality but still enforce that other parts be customized.
4️⃣ For polymorphism
You can create different objects but treat them the same way via a
reference to the abstract class.
5️⃣ For avoiding duplicate code
Put common fields, methods, and constructors in the abstract class
so all subclasses inherit them without rewriting.
✅ Real-world analogy
Think of an abstract class like a blueprint for houses:
The blueprint says every house must have doors, windows, and a roof
(abstract methods).
But it can also say "all houses will have a concrete foundation" (non-
abstract methods).
Different builders can still design the house in their own way.
In C#, an interface is like a contract that defines a set of methods, properties,
events, or indexers without any implementation.
A class or struct that implements an interface must provide the implementation
for all members declared in the interface.
Key Points about Interfaces in C#:
Declared using the interface keyword.
Cannot contain fields or constructors.
Members are implicitly public and cannot have access modifiers.
Supports multiple inheritance (a class can implement multiple interfaces).
Perfect for 100% abstraction.
Used for loose coupling and dependency injection in real projects.
using System;
interface IShape
{
double GetArea();
void Display();
}
class Circle : IShape
{
private double radius;
public Circle(double r)
{
radius = r;
}
public double GetArea()
{
return Math.PI * radius * radius;
}
public void Display()
{
Console.WriteLine("This is a circle.");
}
}
class Program
{
static void Main()
{
IShape shape1 = new Circle(5);
shape1.Display();
Console.WriteLine($"Circle Area: {shape1.GetArea()}");
}
}
Why Interfaces are Used
1. Achieve full abstraction — No method implementation inside an interface.
2. Multiple inheritance — A class can implement many interfaces.
3. Loose coupling — Code depends on contracts, not concrete classes (important for
scalable systems).
4. Polymorphism — Different classes can be accessed via the same interface type.
5. Dependency Injection — Makes unit testing easier and code more flexible.
💡 Real-world analogy:
An interface is like a job description.
The job description lists the tasks you must perform (methods) but doesn’t say how
you should do them — that’s up to you.
Different employees (classes) can do the same job in different ways.
Abstract Class vs Interface in C#
Feature Abstract Class Interface
Keyword abstract interface
Object creation Cannot be instantiated Cannot be instantiated
Only method signatures (until C#
Can have both abstract (no body)
Method 8.0 — now can have default
and non-abstract (with body)
Implementation implementations, but still mostly
methods
signatures)
All members are public by default
Can have any access modifier
Access Modifiers (cannot have access modifiers before
(public, protected, private)
C# 8.0)
Cannot have instance fields (can have
Can have fields, constants,
Fields constants & static members from C#
constructors
8.0)
Multiple A class can inherit only one A class can implement multiple
Inheritance abstract class interfaces
Used when classes share common Used to define a contract that
Purpose base functionality but also need multiple classes can implement,
to enforce some method overrides without sharing code
Can have constructors (but cannot
Constructor Cannot have constructors
directly instantiate)
More flexible for versioning —
Adding new members to an interface
adding new members doesn’t
Version Support breaks all implementing classes (until
break child classes if they are
C# 8 default methods)
non-abstract
Achieved via abstract base
Polymorphism Achieved via interface reference
reference
When you have "is-a" When you need "can-do" capability
When to Use
relationship and shared code across unrelated classes
✅ Interview Tip:
A class can inherit one abstract class but can implement multiple
interfaces.
So, use an abstract class for common base logic and interfaces for
shared contracts across different hierarchies.
If you want, I can also give you a real-world oil & gas engineering example
showing when to use abstract classes vs interfaces, so it becomes more domain-
relevant for you. That could help you answer scenario-based interview questions.
When I say "contract" in C#, I mean:
A formal agreement that says: “Any class implementing this must
provide these members with these exact signatures.”
Why we call it a contract
An interface in C# only defines what must exist (method names,
parameters, return types, properties, events) — it does not define how
they work.
Any class or struct that implements that interface is bound by contract
to implement all the members.
If the class doesn’t implement them → the compiler throws an error.