Roadmap › 1. .NET › Step 10
OOP
OOP Concepts in C#
Classes and objects, the four pillars (encapsulation, abstraction, inheritance, polymorphism), interfaces vs abstract classes, composition and object relationships.
Quick revision for interview day. Print it or save it as a PDF.
Four pillars in one line each
- Encapsulation — private data, public methods that guard rules.
- Abstraction — interfaces/abstract classes expose what, hide how.
- Inheritance —
Derived : Basereuses and extends ("is-a"). - Polymorphism — overloading (compile time) and overriding (runtime).
Interface vs abstract class
| Interface | Abstract class | |
|---|---|---|
| Inherit many? | Yes | No (one) |
| State/fields | No | Yes |
| Constructor | No | Yes |
| Default implementation | Yes (C# 8+) | Yes |
| Best for | Contracts, DI, mocking | Shared base code |
Keywords
| Keyword | Meaning |
|---|---|
virtual | May be overridden |
abstract | Must be overridden; no body |
override | Replaces base virtual/abstract |
sealed | No further inheritance/override |
new | Hides a base member (no polymorphism) |
base | Access base members/constructor |
this | Current instance / chain constructors |
static | Belongs to the type |
Relationships
- Association: uses (independent lifetimes)
- Aggregation: has-a (part survives)
- Composition: owns-a (part dies with whole)
- Inheritance: is-a
- Prefer composition; inherit only for true substitutable "is-a".
Constructor order
Static field initialisers → static ctor (once) → instance field initialisers (derived, then base) → base ctor body → derived ctor body.
Equality checklist
- Override
Equals(object), implementIEquatable<T>, overrideGetHashCodewithHashCode.Combine. - Or use
recordfor value equality for free.