OOPs Interview Questions and Answers in C#
A curated set of frequently asked object-oriented programming interview questions in C#, with clear, concise answers.
Why This Topic Matters
Object-oriented programming concepts are among the most commonly tested areas in C# interviews, since they form the foundation of how C# applications are structured.
Core Concepts Covered
Common interview questions revolve around the four pillars of OOP, encapsulation, abstraction, inheritance, and polymorphism, along with practical examples of each.
Typical Question Style
Interviewers often ask you to explain a concept and then demonstrate it with a short code example, testing both theoretical understanding and practical application.
How to Prepare
Reviewing the core OOP pillars alongside related topics like constructors, access modifiers, and interfaces will prepare you for most commonly asked OOP interview questions.
Frequently Asked Questions with Short Answers
1. What is the difference between an abstract class and an interface?
An abstract class can hold fields, constructors and fully written methods, and a class can inherit from only one. An interface describes a contract; a class can implement many interfaces. Since C# 8, interfaces can also contain default method bodies, but they still cannot hold instance fields.
2. Overloading vs overriding?
Overloading means several methods with the same name but different parameter lists in the same class, chosen at compile time. Overriding means a derived class replaces a virtual or abstract method of its base class using override, chosen at run time.
3. What does the new keyword do on a method?
It hides a base method instead of overriding it. The version called then depends on the variable's declared type, not the actual object, which surprises many developers.
4. What is a sealed class?
A class that cannot be inherited. You can also seal an overridden method so further subclasses cannot override it again.
5. How does C# support encapsulation?
Through access modifiers and properties. Fields stay private, and properties with get and set (or init) control how values are read and changed, often with validation.
6. Can a static class be inherited or instantiated?
No to both. A static class only holds static members and is useful for helper methods.
A Code Question You Should Be Able to Explain
Interviewers often show a short program and ask for its output. This one tests overriding, hiding and polymorphism together.
class Animal
{
public virtual string Speak() => "...";
public string Kind() => "Animal";
}
class Dog : Animal
{
public override string Speak() => "Woof";
public new string Kind() => "Dog";
}
Animal a = new Dog();
Console.WriteLine(a.Speak()); // Woof (override: runtime type decides)
Console.WriteLine(a.Kind()); // Animal (new hides: declared type decides)
Dog d = (Dog)a;
Console.WriteLine(d.Kind()); // Dog
Explain the reasoning aloud: Speak is virtual, so the call goes to the real object type. Kind is hidden, so the compiler binds it to the declared type of the variable.
Follow-Up Questions to Prepare
- Constructor chaining: use
: this(...)to call another constructor in the same class and: base(...)to call the parent constructor. - Composition vs inheritance: prefer composition ("has a") when the relationship is not truly "is a"; it keeps classes loosely coupled.
- Abstraction in practice: depending on an interface such as
IPaymentServicelets you swap implementations and write unit tests with fakes. - Struct vs class: structs are value types, best for small immutable data; classes are reference types.
- SOLID principles: be ready with one real example for at least single responsibility and dependency inversion.
How to Answer Well
- Give a one-line definition first.
- Add a tiny code example or a real situation from your own work.
- Mention a trade-off or common mistake, which shows deeper understanding.
Avoid textbook-only answers such as "a car has wheels". Interviewers value examples drawn from actual projects, such as a repository interface, a notification service with several channels, or a base controller shared by many endpoints.