
Object-oriented programming questions dominate Java interviews. Interviewers use them to gauge whether you understand core concepts or just memorized syntax. This guide covers the OOP questions you’re most likely to face, with clear explanations and code examples.
The Four Pillars Questions
Almost every Java interview starts here. Know these cold.
What are the four pillars of OOP?
The four pillars are encapsulation, inheritance, polymorphism, and abstraction.
Encapsulation bundles data and methods that operate on that data within a single unit (a class), and restricts direct access to some components. Private fields with public getters and setters are the classic example.
Inheritance allows a class to inherit properties and methods from another class. The child class extends the parent, gaining its functionality while adding or overriding behavior.
Polymorphism means “many forms.” A single interface can represent different underlying types. A method can behave differently based on the object that calls it.
Abstraction hides complex implementation details and exposes only what’s necessary. Abstract classes and interfaces define what something does without specifying how.
Explain encapsulation with an example.
public class BankAccount {
private double balance; // Hidden from outside access
public BankAccount(double initialBalance) {
if (initialBalance >= 0) {
this.balance = initialBalance;
}
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
}
The balance field is private. Outside code can’t set it to -1000 or any invalid value. All access goes through methods that enforce business rules. This protects data integrity.
What’s the difference between abstraction and encapsulation?
Encapsulation is about hiding data. You make fields private and control access through methods.
Abstraction is about hiding complexity. You define what operations are available without revealing how they work internally.
A car provides abstraction: you use the steering wheel and pedals without knowing how the engine works. The engine itself uses encapsulation: its internal components are hidden inside a sealed unit.
Inheritance Questions
What is inheritance and why use it?
Inheritance lets a class acquire properties and methods from another class. The child class (subclass) extends the parent class (superclass).
public class Animal {
protected String name;
public void eat() {
System.out.println(name + " is eating");
}
}
public class Dog extends Animal {
public Dog(String name) {
this.name = name;
}
public void bark() {
System.out.println(name + " says woof!");
}
}
Benefits include code reuse (Dog doesn’t redefine eat()), establishing hierarchies that model real relationships, and enabling polymorphism.
Does Java support multiple inheritance?
Java does not support multiple inheritance of classes. A class can extend only one parent class.
// This is NOT allowed in Java
public class Child extends Parent1, Parent2 { } // Compile error
This restriction avoids the “diamond problem” where a class could inherit conflicting implementations from multiple parents.
However, Java supports multiple inheritance of interfaces. A class can implement any number of interfaces:
public class SmartPhone implements Camera, Phone, MediaPlayer {
// Must implement methods from all three interfaces
}
What is the diamond problem?
The diamond problem occurs when a class inherits from two classes that share a common ancestor.
// Hypothetical - not valid Java
class A {
void display() { System.out.println("A"); }
}
class B extends A {
void display() { System.out.println("B"); }
}
class C extends A {
void display() { System.out.println("C"); }
}
class D extends B, C { } // Which display() does D inherit?
If D inherits from both B and C, and both override display(), which version does D get? Java avoids this ambiguity by prohibiting multiple class inheritance.
What’s the difference between extends and implements?
extends is used for class inheritance. A class extends one other class.
implements is used for interfaces. A class implements one or more interfaces.
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable {
// Extends one class, implements multiple interfaces
}
Polymorphism Questions
What is polymorphism? Give an example.
Polymorphism allows objects of different classes to be treated as objects of a common superclass. The same method call produces different behavior depending on the actual object type.
public class Animal {
public void makeSound() {
System.out.println("Some sound");
}
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
public class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
// Polymorphism in action
Animal myPet = new Dog();
myPet.makeSound(); // Outputs: Woof!
myPet = new Cat();
myPet.makeSound(); // Outputs: Meow!
The variable type is Animal, but the actual behavior comes from the concrete class (Dog or Cat).
What’s the difference between compile-time and runtime polymorphism?
Compile-time polymorphism (static binding) is method overloading. The compiler determines which method to call based on the method signature.
public 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;
}
}
Runtime polymorphism (dynamic binding) is method overriding. The JVM determines which method to call at runtime based on the actual object type.
Animal animal = new Dog(); // Decided at runtime
animal.makeSound(); // Calls Dog's version
What is method overloading vs method overriding?
Overloading: Same method name, different parameters, same class.
public class Printer {
public void print(String s) { }
public void print(int i) { }
public void print(String s, int copies) { }
}
Overriding: Same method name, same parameters, different class (subclass redefines parent method).
public class Animal {
public void speak() {
System.out.println("...");
}
}
public class Dog extends Animal {
@Override
public void speak() {
System.out.println("Woof!");
}
}
Can you override a static method?
No. Static methods belong to the class, not instances. You can define a static method with the same signature in a subclass, but it’s method hiding, not overriding.
public class Parent {
public static void greet() {
System.out.println("Hello from Parent");
}
}
public class Child extends Parent {
public static void greet() {
System.out.println("Hello from Child");
}
}
Parent p = new Child();
p.greet(); // Outputs: Hello from Parent (not Child!)
The method called depends on the reference type, not the object type. This is the opposite of how overridden instance methods work.
Abstract Classes and Interfaces
What’s the difference between abstract classes and interfaces?
Abstract classes:
- Can have both abstract and concrete methods
- Can have instance variables (any access modifier)
- Can have constructors
- A class can extend only one abstract class
- Use when classes share code and state
Interfaces:
- Methods are implicitly public and abstract (before Java 8)
- Can have default and static methods (Java 8+)
- Variables are implicitly public, static, and final
- No constructors
- A class can implement multiple interfaces
- Use to define contracts without implementation
When would you use an abstract class over an interface?
Use an abstract class when:
- Subclasses share common code (not just method signatures)
- You need non-public members
- You need instance variables that aren’t constants
- You want to provide a partial implementation
public abstract class Vehicle {
protected int speed;
public void accelerate(int amount) {
speed += amount; // Shared implementation
}
public abstract void steer(); // Subclasses must implement
}
Use an interface when:
- You’re defining a capability that unrelated classes might have
- You need multiple inheritance
- You want to specify a contract without any implementation
public interface Printable {
void print();
}
// Unrelated classes can be Printable
public class Document implements Printable { }
public class Image implements Printable { }
public class Report implements Printable { }
Can an interface extend another interface?
Yes. An interface can extend one or more interfaces.
public interface Drawable {
void draw();
}
public interface Colorable {
void setColor(String color);
}
public interface Shape extends Drawable, Colorable {
double getArea();
}
A class implementing Shape must implement methods from all three interfaces.
Class Design Questions
What is a constructor? Can you override a constructor?
A constructor initializes a new object. It has the same name as the class and no return type.
public class Person {
private String name;
public Person() {
this.name = "Unknown";
}
public Person(String name) {
this.name = name;
}
}
Constructors cannot be overridden because they’re not inherited. Each class has its own constructors. However, constructors can be overloaded (multiple constructors with different parameters).
What is the super keyword?
super refers to the parent class. Use it to:
Call the parent constructor:
public class Dog extends Animal {
public Dog(String name) {
super(name); // Calls Animal's constructor
}
}
Call a parent method:
public class Dog extends Animal {
@Override
public void eat() {
super.eat(); // Call Animal's eat() first
System.out.println("Dog finished eating");
}
}
What is the this keyword?
this refers to the current object. Common uses:
Distinguish instance variables from parameters:
public class Person {
private String name;
public Person(String name) {
this.name = name; // this.name is the field, name is the parameter
}
}
Call another constructor:
public class Person {
private String name;
private int age;
public Person() {
this("Unknown", 0); // Calls the other constructor
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
What are access modifiers and what do they mean?
Access modifiers control visibility:
private→ accessible only within the same classdefault(no modifier) → accessible within the same packageprotected→ accessible within the same package and by subclassespublic→ accessible from anywhere
public class Example {
private int a; // Only this class
int b; // This package only
protected int c; // This package + subclasses
public int d; // Everyone
}
What is the final keyword?
final prevents modification:
Final variable: value cannot change after initialization (constant)
final int MAX_SIZE = 100;
Final method: cannot be overridden by subclasses
public final void criticalMethod() { }
Final class: cannot be extended
public final class String { } // String is final in Java
Object Class Questions
What methods does every Java class inherit from Object?
Every class extends Object implicitly. Key methods include:
toString()→ returns a string representationequals(Object obj)→ tests equalityhashCode()→ returns a hash code valuegetClass()→ returns the runtime classclone()→ creates a copy of the object
Why should you override equals() and hashCode() together?
The contract states: if two objects are equal according to equals(), they must have the same hashCode(). Collections like HashMap and HashSet depend on this.
public class Person {
private String name;
private int age;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Person person = (Person) obj;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
If you override equals() but not hashCode(), two “equal” objects could end up in different hash buckets, breaking HashMap behavior.
Practical Tips for OOP Interviews
Don’t just define terms. Interviewers want to see that you understand when and why to use each concept. Explain your design decisions.
Draw diagrams if allowed. Class hierarchies and relationships are easier to explain visually.
Use real-world analogies carefully. They help explain concepts but can backfire if the analogy breaks down. Know the limitations of your examples.
Be ready to write code. Verbal explanations aren’t enough. Practice implementing interfaces, extending classes, and overriding methods by hand.
Admit when you don’t know something. Guessing wrong is worse than saying “I’m not sure, but I’d approach it by…” Interviewers respect intellectual honesty.
Related: 10 Java Interview Questions | Introduction to OOP
Sources
- Oracle. “Object-Oriented Programming Concepts.” The Java Tutorials. docs.oracle.com
- Bloch, Joshua. Effective Java. Addison-Wesley, 2018.

