👉 Full lesson with practice questions on DevLadder (free, lesson 2 of 15 in the OOP Concepts in C# course)
public class Logger
{
public static readonly DateTime LoadedAt;
static Logger() { LoadedAt = DateTime.UtcNow; } // static ctor: once per type, before first use
public string Name { get; }
public Logger() : this("default") { } // chaining to another ctor
public Logger(string name) { Name = name; }
}
public class Settings
{
private Settings() { } // private ctor: no outside 'new'
public static Settings Load() => new Settings(); // factory method / Singleton
}
public class Admin : User
{
public Admin(string email) : base(email) { } // call base constructor
}
- If you write no constructor, C# provides a parameterless one. Writing any constructor removes it.
- Order: static fields → static ctor (once) → instance field initialisers → base ctor → derived ctor body.
- Copy constructor: a constructor taking another instance of the same type (records generate one).
Interview questions on OOP Concepts in C
What are the four pillars of OOP? Explain each with an example.
-
Encapsulation: private state with a controlled public API.
BankAccountkeeps_balanceprivate and exposesDeposit()that rejects negative amounts. -
Abstraction: expose what, hide how.
IPaymentGateway.ChargeAsync()hides HTTP calls, signatures and retries. -
Inheritance: a derived class reuses and extends a base.
Contractor : EmployeereusesNameandDescribe(). -
Polymorphism: the same call behaves differently by type.
employee.MonthlyPay()runs thePermanentorContractoroverride at runtime.
What is the difference between a class and an object?
A class is a type definition (blueprint) describing fields, properties and methods; it exists in code and metadata. An object is an instance of that class created at runtime with new, occupying memory with its own state. One class, many objects.
What is the difference between method overloading and method overriding?
| Overloading | Overriding |
|---|---|
| Same name, different parameters | Same signature in a derived class |
| Same class (or derived) | Requires virtual/abstract + override
|
| Resolved at compile time | Resolved at runtime by the object's type |
| Return type alone can't differ | Return type the same (covariant returns allowed since C# 9) |
Read the full lesson with progress tracking and the rest of the course on DevLadder: free step-by-step tutorials and interview questions for developers.
Next in the course: Access modifiers →
Top comments (0)