Java Interview Preparation

OOP Interview Questions

Classes, objects, encapsulation, inheritance, polymorphism, abstraction, and Java design principles.

OOP Interview Focus

Classes, objects, encapsulation, inheritance, polymorphism, abstraction, and Java design principles.

OOP interview questions

Interview questions

60 matching

1. What is Object-Oriented Programming? Beginner

Object-Oriented Programming organizes software around classes and objects. Objects combine state, represented by fields, with behavior, represented by methods.

class Car {
  String brand;
  void start() { System.out.println("Car started"); }
}

2. What are the four main principles of OOP? Beginner

The four principles are encapsulation, inheritance, polymorphism, and abstraction. Together they help Java applications remain reusable, maintainable, secure, and extensible.

3. What is a class in Java? Beginner

A class is a blueprint used to create objects. It defines the fields and methods that instances of the class can contain.

class Employee {
  int employeeId;
  String employeeName;
  void work() { }
}

4. What is an object in Java? Beginner

An object is an instance of a class. It is created at runtime and contains actual values for the state defined by its class.

Employee employee = new Employee();

5. What is the difference between a class and an object? Beginner

A class is a logical blueprint that defines fields and methods. An object is a real runtime instance containing actual values.

6. What is encapsulation in Java? Beginner

Encapsulation wraps data and behavior inside a class and controls access to the data. Private fields with public behavior methods are a common implementation.

class BankAccount {
  private double balance;
  public void deposit(double amount) { if (amount > 0) balance += amount; }
  public double getBalance() { return balance; }
}

7. Why should fields normally be private? Beginner

Private fields prevent uncontrolled modification. Methods can validate input and preserve the class invariants before changing internal state.

private double balance;
public void deposit(double amount) {
  if (amount <= 0) throw new IllegalArgumentException();
  balance += amount;
}

8. What is inheritance in Java? Beginner

Inheritance allows a child class to reuse and extend the fields and methods of a parent class using the extends keyword.

class Vehicle { void start() {} }
class Car extends Vehicle { void drive() {} }

9. What are the benefits of inheritance? Beginner

Inheritance reduces duplicate code, provides a natural relationship between related types, enables extension, and supports runtime polymorphism. It should be used only for a genuine is-a relationship.

10. What types of inheritance does Java support? Beginner

Java supports single, multilevel, and hierarchical inheritance through classes. Multiple inheritance of classes is not supported, but a class can implement multiple interfaces.

11. Why does Java not support multiple inheritance through classes? Intermediate

Multiple class inheritance can create ambiguity when parent classes define the same method, known as the diamond problem. Java avoids this by allowing one direct superclass and multiple interfaces.

12. What is polymorphism in Java? Beginner

Polymorphism means one contract or method name can represent different behavior. Java provides compile-time polymorphism through overloading and runtime polymorphism through overriding.

13. What is compile-time polymorphism? Beginner

Compile-time polymorphism is method overloading. The compiler selects the method using the number, types, and order of the arguments.

class Calculator {
  int add(int a, int b) { return a + b; }
  double add(double a, double b) { return a + b; }
}

14. What is runtime polymorphism? Intermediate

Runtime polymorphism is method overriding. A parent reference can point to a child object, and Java selects the child implementation at runtime.

Payment payment = new CreditCardPayment();
payment.processPayment();

15. What is method overloading? Beginner

Overloading defines methods with the same name but different parameter lists. Changing only the return type is not valid overloading.

void print(String value) {}
void print(int value) {}

16. What is method overriding? Intermediate

Overriding occurs when a child class provides a compatible implementation of an inherited parent method. The child method must respect Java access and return-type rules.

class EmailNotification extends Notification {
  @Override void send() { System.out.println("Email"); }
}

17. What is the difference between overloading and overriding? Beginner

Overloading uses different parameters and is resolved at compile time. Overriding requires inheritance, the same parameters, and is resolved at runtime.

18. What is abstraction in Java? Beginner

Abstraction hides implementation details and exposes only the operations a caller needs. Java uses abstract classes and interfaces to model abstractions.

19. What is an abstract class? Beginner

An abstract class cannot be instantiated directly. It can contain fields, constructors, concrete methods, and abstract methods that subclasses must implement.

abstract class Employee {
  abstract double calculateSalary();
  void displayName() {}
}

20. What is an interface in Java? Beginner

An interface defines a contract. Classes implement it and can implement multiple interfaces, which provides flexible abstraction without multiple class inheritance.

interface PaymentService {
  void processPayment(double amount);
}

21. What is the difference between an abstract class and an interface? Intermediate

An abstract class can share state, constructors, and implementation with closely related classes. An interface defines a contract that unrelated classes can implement, and a class can implement multiple interfaces.

22. Can an abstract class have a constructor? Beginner

Yes. Its constructor runs as part of creating a child object and initializes the parent portion of that object.

abstract class Animal {
  Animal() { System.out.println("Animal constructor"); }
}

23. Can an interface have methods with implementation? Intermediate

Yes. Modern Java interfaces can contain default, static, and private methods in addition to abstract methods.

interface Logger {
  default void log(String message) { System.out.println(message); }
}

24. What is the this keyword in Java? Beginner

this refers to the current object. It distinguishes fields from parameters, calls current-class methods or constructors, and can pass the current object to another method.

Student(String name) { this.name = name; }

25. What is the super keyword in Java? Beginner

super refers to the immediate parent class. It accesses parent members and calls a parent constructor, which must be the first constructor statement.

class Car extends Vehicle {
  Car() { super("Car"); }
}

26. What is association in Java? Beginner

Association is a relationship between independent objects, such as a teacher teaching students. Both objects can exist independently.

27. What is aggregation? Intermediate

Aggregation is a weak has-a relationship. The contained object is created outside the container and can continue to exist independently.

class Department {
  private final List<Employee> employees;
  Department(List<Employee> employees) { this.employees = employees; }
}

28. What is composition? Intermediate

Composition is a strong has-a relationship where the container owns the lifecycle of the contained object.

class House {
  private final List<Room> rooms = List.of(new Room("Bedroom"));
}

29. What is the difference between aggregation and composition? Intermediate

Aggregation allows child objects to exist independently and does not fully control their lifecycle. Composition makes the parent responsible for creating and owning the child objects.

30. What is an is-a relationship? Beginner

An is-a relationship represents inheritance. For example, Dog extends Animal means a Dog is an Animal.

class Dog extends Animal { }

31. What is a has-a relationship? Beginner

A has-a relationship represents association, aggregation, or composition. For example, a Car has an Engine when Engine is a field of Car.

class Car {
  private final Engine engine = new Engine();
}

32. What is constructor overloading? Beginner

Constructor overloading provides multiple ways to create an object by defining constructors with different parameter lists.

Product() {}
Product(int id, String name) {}

33. Can constructors be overridden? Beginner

No. Constructors are not inherited and belong to their own class. A child constructor can call a parent constructor using super().

34. What is dynamic method dispatch? Intermediate

Dynamic method dispatch is Java’s runtime mechanism for selecting an overridden method based on the actual object rather than the reference type.

Animal animal = new Dog();
animal.makeSound();

35. What is upcasting? Beginner

Upcasting assigns a child object to a parent reference. It is safe, automatic, and commonly used for runtime polymorphism.

Animal animal = new Dog();

36. What is downcasting? Intermediate

Downcasting converts a parent reference back to a child reference. It must be checked carefully because an incompatible object causes ClassCastException.

if (animal instanceof Dog dog) { dog.makeSound(); }

37. Can static methods be overridden? Intermediate

No. Static methods belong to the class. A child declaration with the same signature hides the parent method; the selected method depends on the reference type.

38. Can private methods be overridden? Beginner

No. Private methods are accessible only inside their declaring class and are not inherited. A same-named child method is independent.

39. Can final methods be overridden? Beginner

No. A final method cannot be changed by a subclass. Use final when the implementation must remain fixed.

40. What is a final class? Beginner

A final class cannot be extended. Java’s String class is a common example.

final class SecurityUtil { }

41. What is loose coupling? Intermediate

Loose coupling means depending on abstractions rather than concrete implementations. It improves testing, maintenance, extension, and replacement of dependencies.

class OrderService {
  private final NotificationService notifications;
  OrderService(NotificationService notifications) { this.notifications = notifications; }
}

42. What is cohesion? Intermediate

Cohesion describes how closely related a class’s responsibilities are. High cohesion means the class has one focused purpose and is easier to test and maintain.

43. What is an immutable class? Intermediate

An immutable object cannot change after creation. Use final fields, constructor initialization, no setters, defensive copies, and no exposed mutable state.

public final class Employee {
  private final int id;
  private final String name;
  public Employee(int id, String name) { this.id = id; this.name = name; }
}

44. Why is composition often preferred over inheritance? Intermediate

Composition avoids unnecessary parent-child coupling and allows implementations to be replaced or tested independently. Use inheritance only when the child truly is a parent type.

45. How is OOP used in Spring Boot applications? Intermediate

Spring Boot uses encapsulated entities and DTOs, service interfaces for abstraction, base types for selective inheritance, polymorphic implementations, and composition through dependency injection.

46. How would you design a payment system using OOP? Advanced

Define a PaymentStrategy interface, implement credit-card, UPI, and wallet strategies, and inject the selected strategy into a PaymentProcessor. This demonstrates abstraction, polymorphism, composition, and loose coupling.

interface PaymentStrategy { void pay(double amount); }
class PaymentProcessor {
  private final PaymentStrategy strategy;
  PaymentProcessor(PaymentStrategy strategy) { this.strategy = strategy; }
}

47. How would you add a new payment type without modifying existing code? Advanced

Create a new class that implements the existing payment interface. The processor remains unchanged, following the Open/Closed Principle.

class WalletPayment implements PaymentStrategy {
  public void pay(double amount) { }
}

48. Is Java a completely object-oriented language? Beginner

No. Java supports primitive types such as int, double, char, and boolean, which are not objects. Wrapper classes provide object representations when needed.

49. Can we create an object without using new? Intermediate

Yes. Objects can be obtained through factory methods, reflection, cloning, deserialization, or dependency injection. For example, LocalDate.now() returns an object without application code calling new.

LocalDate date = LocalDate.now();

50. Can an abstract class be final? Beginner

No. Abstract means the class is designed to be extended, while final prevents extension. Using both creates a contradiction and does not compile.

51. Can a constructor be private? Beginner

Yes. Private constructors prevent direct creation and are useful for utility classes, singleton implementations, and factory-controlled creation.

class ApplicationConfig {
  private ApplicationConfig() { }
}

52. Can we override an overloaded method? Intermediate

Yes. Overloading and overriding are separate. A child can override one overloaded method while inheriting the other overloads.

53. What are the four pillars of OOP? Beginner

The four pillars are encapsulation, inheritance, polymorphism, and abstraction.

54. What is the difference between an interface and an abstract class? Beginner

An interface defines a contract that multiple classes can implement. An abstract class provides shared state or implementation for a closely related class hierarchy.

55. What is the role of private fields and public methods in OOP? Beginner

Private fields protect internal state while public methods expose controlled behavior. This combination supports encapsulation, validation, and maintainability.

56. What is the relationship between loose coupling and dependency injection? Intermediate

Dependency injection supplies abstractions from outside a class, so the class does not construct concrete dependencies. This is a practical way to achieve loose coupling.

57. What is the difference between state and behavior in an object? Beginner

State is the data held by an object, such as an employee name. Behavior is the work the object performs through methods, such as calculating salary.

58. What is the Open/Closed Principle? Intermediate

Software entities should be open for extension but closed for modification. Interfaces and polymorphic implementations allow new behavior without changing stable client code.

59. What is high cohesion and why is it important? Intermediate

High cohesion keeps closely related responsibilities together and unrelated responsibilities separate. It produces focused classes that are easier to understand, test, and change.

60. What are common OOP best practices in Java? Advanced

Keep fields private, expose behavior instead of mutable state, prefer constructor injection, program to interfaces, favor composition, keep classes focused, use immutable objects, and avoid deep inheritance hierarchies.

How to Prepare for Java Technical Interviews

Use this Java technical interview question bank to revise core concepts and practise explaining your decisions clearly. The questions cover Java fundamentals, object-oriented programming, collections, exceptions, multithreading, Spring Boot, Hibernate, databases, and other topics used in real software development interviews.

Start with the fundamentals, then move to scenario-based questions and advanced topics. Filter questions by difficulty to build confidence gradually. A strong answer should define the concept, explain why it matters, and include a practical example when appropriate.

Continue your preparation with the Java tutorials, or explore all interview preparation tracks for managerial and company-focused questions.