As you're aware, OOP (Object-Oriented Programming) is foundational in enterprise software development. Whether you're working in C#, Java, or C++, mastering OOP is essential—not just for writing maintainable code, but also for performing well in technical interviews. Below are some of the most commonly asked OOP interview questions, focused on C# but also applicable in simpler forms to Java and C++
1. Explain what are four pillar of OOPS?
Ans :- The four foundational principles—commonly referred to as the four pillars of Object-Oriented Programming
- Encapsulation Encapsulation is the practice of bundling data (attributes) and the methods that operate on that data into a single class, while also restricting access to the internal representation.
As an SDE, you'll often use encapsulation to ensure that your objects expose only what they need to—through controlled interfaces (like getBalance() and deposit(amount)), while keeping the sensitive state (like balance) private and protected. This keeps the system modular, secure, and maintainable
class BankAccount {
private double balance;
public double getBalance() { return balance; }
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
}
- Inheritance Inheritance allows a class (child) to derive properties and methods from another class (parent), enabling code reuse, logical hierarchy formation, and extensibility.
In enterprise systems, inheritance helps you create clean hierarchies (e.g., Vehicle → Car, Vehicle → Motorcycle) allowing you to define shared behavior once, then specialize as needed—minimizing duplication and enhancing maintainability
class Vehicle {
public void startEngine() {
Console.WriteLine("Vehicle Start");
}
}
class Car extends Vehicle {
void openTrunk() {
Console.WriteLine("Car Truck Open");
}
}
- Abstraction Abstraction focuses on presenting essential features while hiding unnecessary implementation details. It typically manifests via abstract classes or interfaces that define a contract but not the full implementation.
When designing systems—say, a Smart Home API with generic Device operations—it allows you to present a clean interface (turnOn(), turnOff()) across varied device types, with each device implementing the behavior. This enables clarity, flexibility, and extensibility
abstract class Device {
abstract void turnOn();
abstract void turnOff();
}
class Light extends Device { ... }
class Thermostat extends Device { ... }`
- Polymorphism Polymorphism means “many forms.” It allows objects to be treated as instances of their parent class while invoking method implementations that are specific to the actual subtype. It can be compile-time (method overloading) or runtime (method overriding).
In practical scenarios, polymorphism makes your code flexible and generically extensible. For example, a collection of Shape objects can call draw() on each shape, and the correct behavior (circle drawing vs rectangle drawing) happens automatically
class Shape { void draw() { ... } }
class Circle extends Shape { @Override void draw() { ... } }
Shape s = new Circle();
s.draw(); // Executes Circle’s draw()
Why This Order Makes Sense for SDE Interviewers
Essentially, it means you start by protecting your data (encapsulation), then reuse and organize it with hierarchies (inheritance), hide the details behind clean interfaces (abstraction), and finally let your code adapt to different behaviors when needed (polymorphism).
2. Explain difference between abstraction and encapsulation ?
Ans :- Both are two different concept and has a diffrent purpose
Abstraction: Focuses on what a class does by hiding the underlying complexity and exposing only the necessary behavior.
Privatizing the inner workings and wrapping it in a public shell so details can change without impacting how it’s used.
Defines what an object should do, without revealing how it’s implemented:
public abstract class Shape
{
public abstract double Area();
}
public class Circle : Shape
{
public double Radius { get; set; }
public override double Area() => Math.PI * Radius * Radius;
}
Users call Area() without knowing the calculation details.
Encapsulation: Focuses on how a class protects its internal data by bundling it with methods and restricting direct access.Binding data and operations into a class, hiding the internals with access modifiers while exposing a controlled public interface.
Protects internal state and ensures controlled access:
public class BankAccount
{
private decimal balance;
public void Deposit(decimal amount)
{
if (amount > 0) balance += amount;
}
public decimal GetBalance() => balance;
}
The balance field is private and can be changed only via validated methods.
3. Explain different type of Polymorphism?
Ans :- This question can be ask in different why like Explain compile time polymorphism (static) and run time(Dynamic) polymorphism. Here is the detail explanation of it .
- Compile-Time Polymorphism (Static Polymorphism)
What it is: The method to call is determined at compile time.
How it's done: Typically via method overloading (same method name, different parameters) or operator overloading.
public class MathOps
{
public int Add(int a, int b) => a + b;
public float Add(float a, float b) => a + b;
}
Here, the correct Add method is selected by the compiler based on argument types.
- Run-Time Polymorphism (Dynamic Polymorphism)
What it is: The method to call is determined at runtime, based on the object’s actual type.
How it's done: Through method overriding using virtual and override keywords with inheritance.
public class Animal
{
public virtual void Speak() => Console.WriteLine("Animal speaks");
}
public class Dog : Animal
{
public override void Speak() => Console.WriteLine("Dog barks");
}
Animal pet = new Dog();
pet.Speak(); // Outputs: "Dog barks"
Here, the program decides at runtime to call Dog’s Speak() override.
4. Explain what access modifiers and its type?
Ans :- Access modifiers are special keywords in C# that determine who can see or use certain parts of your code—whether it's within the same class, the same assembly (project), or anywhere in the application. They play a key role in encapsulation, ensuring that only the right parts of your code are exposed or hidden.
- public: Accessible from anywhere—across classes and assemblies.
- private: Accessible only within the same class.
- protected: Accessible within the same class and derived classes.
- internal: Accessible only within the same assembly.
5. Explain difference between abstract class and interface?
Ans :-
- Abstract Class
An abstract class is a class that cannot be instantiated on its own; it must be subclassed. It can have both abstract methods (methods without implementation) and normal methods (methods with implementation).
Abstract methods: These are methods that are declared but have no implementation. They are meant to be overridden by subclasses.
Normal methods: These are methods that have a complete implementation in the abstract class and can be used as is by the subclasses.
Key points about Abstract Class:
Can have both abstract and normal methods.
Can have member variables (fields) and constructors.
Can provide a partial implementation of methods, meaning some methods are implemented, but others are left abstract for subclasses to define.
A class can only inherit from one abstract class (single inheritance).
Example of an Abstract Class:
abstract class Animal {
// Abstract method (no implementation)
public abstract void makeSound();
// Normal method (with implementation)
public void sleep() {
System.out.println("The animal is sleeping.");
}
}
class Dog extends Animal {
// Implementing the abstract method
public void makeSound() {
System.out.println("Bark!");
}
}
- Interface
An interface is a contract or blueprint that specifies what methods a class must implement, but it cannot provide any implementation itself (though some languages now allow default method implementations in interfaces). All methods in an interface are abstract by default.
No implementation in interface: The methods in an interface only declare what methods the implementing class should have, but they do not provide any functionality themselves.
Cannot have instance variables: Unlike an abstract class, an interface cannot have member variables (fields). It can only declare constants (static final fields).
Can be implemented by any class: A class can implement multiple interfaces (multiple inheritance), which is one key advantage over abstract classes.
Key points about Interface:
Only abstract methods (no method bodies unless it's a default method in some languages).
Cannot have constructors or instance variables.
Supports multiple inheritance (a class can implement multiple interfaces).
Used when you want to define a common set of methods for classes that might not share a common ancestor.
Example of an Interface:
interface Animal {
void makeSound(); // Abstract method with no implementation
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark!");
}
}
6.Explain virtual and override keyword?
Ans :-
- virtual Keyword Used in the base class. Marks a method that can be overridden in derived classes. Allows a method to have a default implementation, but it can be changed in subclasses.
- override Keyword: Used in the derived class. Replaces the implementation of a virtual method from the base class. Ensures the method in the derived class has a new implementation.
using System;
class Animal
{
// Virtual method: can be overridden by derived classes
public virtual void MakeSound()
{
Console.WriteLine("Some generic animal sound");
}
}
class Dog : Animal
{
// Override the virtual method to provide specific implementation
public override void MakeSound()
{
Console.WriteLine("Bark!");
}
}
class Program
{
static void Main()
{
Animal myDog = new Dog(); // Polymorphism: Animal reference, Dog object
myDog.MakeSound(); // Output: Bark!
}
}
7. What is the difference between method overloading and method overriding?
Ans:
Method Overloading
Occurs at compile time. You define multiple methods in the same class with the same name but different signatures (different number or types of parameters). Overloading is a way to provide multiple ways to call a method.
Example:
class Calculator {
public int Add(int a, int b) { return a + b; }
public double Add(double a, double b) { return a + b; }
public int Add(int a, int b, int c) { return a + b + c; }
}
Method Overriding
Occurs at runtime (dynamic polymorphism). A derived class provides its specific implementation of a method that’s already defined in base class (which is marked virtual, abstract, or override in further parent). To override, you use the override keyword in the derived class.
Example:
class Animal {
public virtual void Speak() { Console.WriteLine("Animal sound"); }
}
class Dog : Animal {
public override void Speak() { Console.WriteLine("Bark"); }
}
8. What is the sealed keyword in C#, and when would you use it?
Ans:-
The sealed keyword can be used in two main contexts:
Sealing a class: If you mark a class as sealed, it cannot be inherited. No class can derive from that class.
public sealed class FinalClass {
public void DoSomething() { … }
}
// public class Derived : FinalClass { } // Not allowed
Sealing an override method: In a derived class, if you override a virtual method, you can mark that override as sealed override. This prevents further derived classes from overriding that method again.
class Base {
public virtual void Display() { … }
}
class Intermediate : Base {
public sealed override void Display() { … }
}
class FurtherDerived : Intermediate {
// public override void Display() { … } // Not allowed
}
When to use sealed:
To prevent inheritance where it doesn’t make sense or might cause incorrect use.
To lock down behavior for security or consistency.
Sometimes for performance reasons (sealed classes are optimized better by compiler/JIT in some cases).
9. What are value types vs reference types in C#? Give examples.
Ans:
Value Types
Stored in the stack (or inline in containing objects)
When you assign one value-type variable to another, it copies the value (they are independent)
Examples: int, double, bool, struct, enum
Reference Types
Stored as references (the value is a reference/pointer) to data stored in the heap
When you assign one reference-type variable to another, both reference the same object. Changes via one variable reflect in the other.
Examples: class objects, string (though it's immutable), arrays, delegates
Why it matters: memory usage; behavior of assignment; garbage collection; performance implications (boxing / unboxing when converting between value/reference types).
10. What is a static class in C#? How is it different from a regular class?
Ans:-
A static class in C# is a class that:
Cannot be instantiated (you can’t create an object using new).
All its members (methods, properties, fields) must be static.
Is implicitly sealed. You cannot derive from a static class.
Differences vs regular class:
Regular class: you can create instances; can have both static and non-static members; subclasses possible.
Static class: only static members; no instances; no inheritance; often used for utility/helper functionality or grouping related static operations.
Example use case: Methods like Math.Sqrt(), or utility methods in a helper class that don’t need object state.
Top comments (0)