Object-Oriented Programming (OOP) Interview
Questions
Basic OOP Concepts
1. What is Object-Oriented Programming (OOP)?
Answer: OOP is a programming paradigm based on the concept of objects which contain
data (attributes) and code (methods). It focuses on creating reusable code through classes
and objects.
2. What are the four main principles of OOP?
Answer: The four pillars of OOP are:
Encapsulation - Bundling data and methods together
Inheritance - Creating new classes based on existing classes
Polymorphism - One interface, multiple implementations
Abstraction - Hiding complex implementation details
3. What is a Class?
Answer: A class is a blueprint or template for creating objects. It defines the properties
(attributes) and behaviors (methods) that objects of that type will have.
4. What is an Object?
Answer: An object is an instance of a class. It's a real-world entity that has state (attributes)
and behavior (methods).
5. What is the difference between Class and Object?
Answer:
Class: Template/blueprint, no memory allocated, defined once
Object: Instance of class, memory allocated, can have multiple instances
Encapsulation Questions
6. What is Encapsulation?
Answer: Encapsulation is the bundling of data (variables) and methods that operate on that
data within a single unit (class), and restricting direct access to some components.
7. How do you achieve Encapsulation?
Answer: Through:
Making variables private
Providing public getter/setter methods
Using access modifiers (private, protected, public)
8. What are Access Modifiers?
Answer:
Private: Accessible only within the same class
Protected: Accessible within class and its subclasses
Public: Accessible from anywhere
Default/Package: Accessible within the same package
9. What are Getter and Setter methods?
Answer:
Getter: Method to retrieve/read private variable values
Setter: Method to set/modify private variable values
They provide controlled access to private data
10. Why use private variables with public methods?
Answer:
Data security and validation
Control over how data is accessed/modified
Maintaining data integrity
Easy to modify implementation without affecting client code
Inheritance Questions
11. What is Inheritance?
Answer: Inheritance is a mechanism where a new class (child/derived) inherits properties
and methods from an existing class (parent/base), promoting code reusability.
12. Types of Inheritance?
Answer:
Single Inheritance: One parent class
Multiple Inheritance: Multiple parent classes (not supported in Java/C#)
Multilevel Inheritance: Chain of inheritance
Hierarchical Inheritance: Multiple children from one parent
Hybrid Inheritance: Combination of multiple types
13. What is Method Overriding?
Answer: When a child class provides a specific implementation of a method that already
exists in its parent class. The child class method overrides the parent class method.
14. What is Method Overloading?
Answer: Having multiple methods with the same name but different parameters (number,
type, or order) within the same class.
15. Difference between Overloading and Overriding?
Answer:
Overloading: Same method name, different parameters, compile-time
Overriding: Same method signature, runtime, inheritance required
16. What is super keyword?
Answer: Used to refer to the immediate parent class object. Used to:
Call parent class methods
Access parent class variables
Call parent class constructor
17. Can we override static methods?
Answer: No, static methods cannot be overridden because they belong to the class, not to
instances. They can be hidden but not overridden.
18. Can we override private methods?
Answer: No, private methods cannot be overridden because they are not visible to child
classes.
Polymorphism Questions
19. What is Polymorphism?
Answer: Polymorphism means "many forms." It allows objects of different types to be
treated as objects of a common base type, with the specific method implementation
determined at runtime.
20. Types of Polymorphism?
Answer:
Compile-time Polymorphism: Method overloading, operator overloading
Runtime Polymorphism: Method overriding, virtual functions
21. What is Dynamic Method Dispatch?
Answer: The mechanism by which a call to an overridden method is resolved at runtime
rather than compile time. The method called depends on the actual object type, not the
reference type.
22. What is upcasting and downcasting?
Answer:
Upcasting: Converting child class reference to parent class reference (automatic)
Downcasting: Converting parent class reference to child class reference (manual,
requires explicit casting)
23. Can we achieve polymorphism without inheritance?
Answer: Yes, through interfaces. Multiple classes can implement the same interface,
providing different implementations of the same method.
Abstraction Questions
24. What is Abstraction?
Answer: Abstraction is hiding the complex implementation details while showing only the
essential features of an object. It focuses on what an object does rather than how it does it.
25. How to achieve Abstraction?
Answer: Through:
Abstract classes
Interfaces
Access modifiers
26. What is an Abstract Class?
Answer: A class that cannot be instantiated and may contain abstract methods (methods
without implementation). It's used as a base class for other classes.
27. What is an Interface?
Answer: An interface is a contract that defines what methods a class must implement. It
contains only method declarations (and constants) without implementation.
28. Difference between Abstract Class and Interface?
Answer:
Abstract Class Interface
Can have concrete methods Only abstract methods (Java 8+ allows default)
Can have constructors Cannot have constructors
Can have instance variables Only constants (public static final)
Single inheritance Multiple inheritance supported
Can have access modifiers Methods are public by default
29. Can an interface extend another interface?
Answer: Yes, interfaces can extend other interfaces using the extends keyword, and they can
extend multiple interfaces.
30. Can a class implement multiple interfaces?
Answer: Yes, a class can implement multiple interfaces, which is how multiple inheritance is
achieved in languages like Java and C#.
Advanced OOP Questions
31. What is Composition?
Answer: Composition is a design technique where a class contains objects of other classes
as instance variables. It represents a "has-a" relationship.
32. Difference between Inheritance and Composition?
Answer:
Inheritance: "is-a" relationship, tight coupling, single inheritance limitation
Composition: "has-a" relationship, loose coupling, more flexible
33. What is Aggregation?
Answer: Aggregation is a special form of association where objects have their own lifecycle
but have ownership. It represents a "has-a" relationship with weaker coupling than
composition.
34. What is Association?
Answer: Association is a relationship between two classes where one class uses or interacts
with another class. It can be one-to-one, one-to-many, or many-to-many.
35. What are Static methods and variables?
Answer: Static members belong to the class rather than instances. They can be accessed
without creating objects and are shared among all instances.
36. What is a Constructor?
Answer: A constructor is a special method used to initialize objects when they are created. It
has the same name as the class and no return type.
37. Types of Constructors?
Answer:
Default Constructor: No parameters
Parameterized Constructor: Takes parameters
Copy Constructor: Creates object from another object
38. What is Constructor Overloading?
Answer: Having multiple constructors with different parameter lists in the same class.
39. What is Method Chaining?
Answer: A technique where multiple methods are called on the same object in a single
statement, with each method returning the object itself.
40. What is the Diamond Problem?
Answer: A problem in multiple inheritance where a class inherits from two classes that have
a common base class, creating ambiguity about which parent's method to inherit.
Design Patterns Questions
41. What is Singleton Pattern?
Answer: A design pattern that ensures only one instance of a class exists and provides
global access to it.
42. What is Factory Pattern?
Answer: A creational pattern that creates objects without specifying their concrete classes,
using a factory method to create objects.
43. What is Observer Pattern?
Answer: A behavioral pattern where an object (subject) notifies multiple dependent objects
(observers) about state changes.
44. What is MVC Pattern?
Answer: Model-View-Controller pattern that separates application logic into three
interconnected components for better organization and maintainability.
Practical OOP Examples
45. Design a Car class with OOP principles
java
// Example implementation
public abstract class Vehicle {
protected String brand;
protected int year;
public Vehicle(String brand, int year) {
[Link] = brand;
[Link] = year;
}
public abstract void start();
public abstract void stop();
public void displayInfo() {
[Link]("Brand: " + brand + ", Year: " + year);
}
}
public class Car extends Vehicle {
private int doors;
public Car(String brand, int year, int doors) {
super(brand, year);
[Link] = doors;
}
@Override
public void start() {
[Link]("Car is starting with key");
}
@Override
public void stop() {
[Link]("Car is stopping");
}
// Getters and setters
public int getDoors() { return doors; }
public void setDoors(int doors) { [Link] = doors; }
}
46. Explain Polymorphism with example
java
// Base class
class Animal {
public void makeSound() {
[Link]("Animal makes a sound");
}
}
// Derived classes
class Dog extends Animal {
@Override
public void makeSound() {
[Link]("Dog barks");
}
}
class Cat extends Animal {
@Override
public void makeSound() {
[Link]("Cat meows");
}
}
// Polymorphism in action
public class Main {
public static void main(String[] args) {
Animal animal1 = new Dog(); // Upcasting
Animal animal2 = new Cat(); // Upcasting
[Link](); // Output: Dog barks
[Link](); // Output: Cat meows
}
}
47. Interface example
java
interface Drawable {
void draw();
default void display() {
[Link]("Displaying shape");
}
}
class Circle implements Drawable {
@Override
public void draw() {
[Link]("Drawing Circle");
}
}
class Rectangle implements Drawable {
@Override
public void draw() {
[Link]("Drawing Rectangle");
}
}
Quick Tips for OOP Interviews
1. Always provide examples when explaining concepts
2. Draw diagrams for inheritance hierarchies
3. Explain real-world scenarios for each concept
4. Know the differences between similar concepts
5. Understand when to use each OOP principle
6. Be ready to write code for common OOP problems
7. Explain advantages and disadvantages of each approach
Common Interview Coding Problems
1. Design a library management system
2. Create a shape hierarchy with area calculation
3. Implement a banking system with different account types
4. Design a vehicle rental system
5. Create an employee management system
6. Implement a game with different character types
7. Design a notification system using Observer pattern
8. Create a file system hierarchy
Remember: OOP is about modeling real-world problems using objects and their
relationships. Always think in terms of real-world examples when answering OOP questions!