OOPs Interview Questions in Java (With Answers)


🔹 1. What is OOPs in Java?

OOPs (Object-Oriented Programming System) is a programming approach based on objects and classes. It helps in organizing code into reusable and modular parts.


🔹 2. What are the 4 pillars of OOPs?

The four main pillars are:

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

🔹 3. What is Encapsulation?

Encapsulation means wrapping data and methods into a single unit (class) and restricting direct access.

Example:

class Student {
private int age;

public void setAge(int a) {
age = a;
}

public int getAge() {
return age;
}
}

👉 It protects data using private variables.


🔹 4. What is Inheritance?

Inheritance allows one class to acquire properties of another class.

Example:

class Animal {
void eat() {
System.out.println("Eating...");
}
}

class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}

🔹 5. Types of Inheritance in Java?

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Multiple inheritance (through interfaces only)

🔹 6. What is Polymorphism?

Polymorphism means one action, many forms.

Types:

  • Compile-time (Method Overloading)
  • Run-time (Method Overriding)

🔹 7. What is Method Overloading?

Same method name but different parameters.

Example:

class Test {
void show(int a) {}
void show(String b) {}
}

🔹 8. What is Method Overriding?

Child class provides specific implementation of parent method.

Example:

class Animal {
void sound() {
System.out.println("Animal sound");
}
}

class Dog extends Animal {
void sound() {
System.out.println("Barking");
}
}

🔹 9. What is Abstraction?

Hiding internal implementation and showing only functionality.


🔹 10. How is abstraction achieved in Java?

  • Abstract class
  • Interface

🔹 11. What is an Abstract Class?

A class that cannot be instantiated and may contain abstract methods.


🔹 12. What is an Interface?

An interface contains only abstract methods (before Java 8) and is used for full abstraction.


🔹 13. Difference between Abstract Class and Interface?

Abstract ClassInterface
Can have constructorsNo constructors
Can have normal methodsOnly abstract (before Java 8)
Supports partial abstractionFull abstraction

🔹 14. Can we achieve multiple inheritance in Java?

No, but it is possible using interfaces.


🔹 15. What is a Class?

A class is a blueprint for objects.


🔹 16. What is an Object?

An object is an instance of a class.


🔹 17. Difference between Class and Object?

ClassObject
BlueprintReal entity
LogicalPhysical

🔹 18. What is Constructor?

A constructor is a special method used to initialize objects.


🔹 19. Types of Constructor?

  • Default constructor
  • Parameterized constructor

🔹 20. Can constructor be overloaded?

Yes, constructors can be overloaded with different parameters.


🔹 21. Can constructor be inherited?

No, constructors are not inherited in Java.


🔹 22. What is this keyword?

It refers to the current class object.


🔹 23. What is super keyword?

It refers to the parent class object.


🔹 24. What is static keyword?

Static means it belongs to the class, not object.


🔹 25. What is final keyword?

Used to restrict:

  • Variable → constant
  • Method → cannot override
  • Class → cannot inherit

🔹 26. What is runtime polymorphism?

Method overriding that is resolved at runtime.


🔹 27. What is compile-time polymorphism?

Method overloading resolved during compilation.


🔹 28. Why Java does not support multiple inheritance with classes?

To avoid ambiguity and complexity (diamond problem).


🔹 29. What is tight coupling?

When classes are highly dependent on each other.


🔹 30. What is loose coupling?

When classes are independent and easier to maintain.


🔹 31. What is object cloning?

Creating a copy of an existing object.


🔹 32. What is constructor chaining?

Calling one constructor from another using this() or super().


🔹 33. What is composition?

Using one class inside another class (HAS-A relationship).


🔹 34. Difference between HAS-A and IS-A relationship?

  • IS-A → Inheritance
  • HAS-A → Composition

🔹 35. What is interface multiple inheritance?

A class can implement multiple interfaces.


🔹 36. Can interface have method implementation?

Yes, from Java 8 (default methods).


🔹 37. What is default method in interface?

A method with body inside interface.


🔹 38. What is marker interface?

An empty interface (e.g., Serializable).


🔹 39. What is upcasting?

Parent class reference pointing to child object.


🔹 40. What is downcasting?

Converting parent reference back to child.


🔹 41. What is dynamic method dispatch?

Runtime polymorphism using method overriding.


🔹 42. What is aggregation?

Weak HAS-A relationship (independent objects).


🔹 43. What is encapsulation advantage?

  • Data security
  • Easy maintenance
  • Code flexibility

🔹 44. What is abstraction advantage?

  • Hides complexity
  • Reduces code duplication

🔹 45. What is inheritance advantage?

  • Code reusability
  • Easy maintenance

🔹 46. What is polymorphism advantage?

  • Flexibility
  • Extensibility

🔹 47. What is method hiding?

Static method in child hides parent method.


🔹 48. Can we override static method?

No, static methods cannot be overridden.


🔹 49. What is object lifecycle?

Creation → Usage → Destruction (Garbage collection)


🔹 50. What is garbage collection?

Automatic memory management in Java.