OOP interview questions in Java (with answers)
16 min readUpdated
Object-oriented programming is the topic almost every Java interviewer starts with. These are the questions they ask most, with answers short enough to say out loud and small programs for the tricky output questions.
Classes and objects
1. What is the difference between a class and an object?
A class is a blueprint: it lists the fields an object will hold and the methods it can run. An object is one real thing built from that blueprint with new, with its own copy of the fields. Student is a class; new Student("Asha") is an object.
2. What is object-oriented programming?
A way of organising code around objects that bundle data and the behaviour that works on that data. Instead of passing raw data to many separate functions, you ask an object to do something: account.withdraw(500).
3. What are the four pillars of OOP?
Encapsulation, inheritance, polymorphism and abstraction. Be ready to explain each in one line and give a small example; interviewers almost always follow up on one of them.
4. What is a constructor, and how is it different from a method?
A constructor runs once when an object is created and sets up its starting state. It has the same name as the class and no return type, not even void. A method can be called any number of times and can return a value.
5. What is the default constructor?
If you write no constructor at all, the compiler adds a public no-argument one that does nothing. As soon as you write any constructor, the default one is no longer added, so new Student() stops compiling unless you write it yourself.
6. What is constructor chaining?
Calling one constructor from another: this(...) calls another constructor of the same class, super(...) calls the parent's. Either call must be the first statement. It avoids repeating setup code.
Encapsulation and access
7. What is encapsulation?
Keeping an object's fields private and allowing changes only through methods that check the rules. A BankAccount with a private balance and a withdraw() method that refuses to go below zero can never end up in a wrong state.
8. What are the access modifiers in Java?
private (only inside the class), no modifier, called package-private (same package), protected (same package plus subclasses) and public (everywhere). Start with the most restrictive one that works.
9. Why use getters and setters instead of public fields?
A setter can validate (age must not be negative), a getter can return a copy instead of the internal list, and you can change how the data is stored later without breaking callers. A public field gives all of that up.
10. How do you make a class immutable?
Make the class final, make all fields private final, set them only in the constructor, provide no setters, and make defensive copies of mutable inputs and outputs such as lists. String and records are immutable.
Inheritance
11. What is inheritance?
A class reusing another class's fields and methods with extends, forming an is-a relationship: a SavingsAccount is an Account. The child can add new members and override inherited methods.
12. What types of inheritance does Java support?
Single (one parent), multilevel (A → B → C) and hierarchical (many children of one parent) for classes. Multiple inheritance is supported only through interfaces.
13. Why does Java not allow multiple inheritance of classes?
To avoid the diamond problem: if two parents had the same method, the child would not know which to inherit. With interfaces, if two default methods clash, the class must override the method and choose explicitly.
14. What is the order of constructor calls in inheritance?
Parent first, then child. Every constructor starts by calling the parent's constructor (with an automatic super() if you do not write one), so the parent part of the object is ready before the child's code runs.
public class ConstructorOrder {
static class Vehicle {
Vehicle() { System.out.println("Vehicle ready"); }
}
static class Car extends Vehicle {
Car() { System.out.println("Car ready"); }
}
static class SportsCar extends Car {
SportsCar() { System.out.println("SportsCar ready"); }
}
public static void main(String[] args) {
new SportsCar();
}
}Output
Vehicle ready Car ready SportsCar ready
15. What is the difference between composition and inheritance? Which is better?
Inheritance is is-a (Car extends Vehicle); composition is has-a (Car has an Engine field). Prefer composition in most designs: it is more flexible, you can swap the part at run time, and changes in a parent class cannot silently break the child.
16. Can you override a private or static method?
No. A private method is not visible to the child, so a method with the same name is a new method. A static method with the same signature in the child hides the parent's one: which one runs depends on the variable's type, not the object's.
17. What does final do on a class and on a method?
A final class cannot be extended (String, Integer). A final method cannot be overridden. Use them when a subclass changing the behaviour would break correctness or security.
Polymorphism
18. What is polymorphism?
One call, many behaviours. shape.area() runs the circle's or the square's version depending on the actual object. It lets you write code against a general type and add new types later without changing that code.
19. What is the difference between compile-time and run-time polymorphism?
Compile-time polymorphism is method overloading: the compiler picks the method from the argument types. Run-time polymorphism is method overriding: the JVM picks the method from the actual object (dynamic method dispatch).
20. What are the rules for overriding a method?
Same name and parameters; the return type can be the same or a subtype (covariant return); access cannot be more restrictive; it cannot throw broader checked exceptions; and @Override should always be used so the compiler catches mistakes.
21. Which method runs: the one of the variable's type or the object's type?
For instance methods, the object's type decides. For fields and static methods, the variable's type decides, because they are not polymorphic. This is a favourite output question.
public class DispatchPuzzle {
static class Parent {
String name = "parent field";
static String kind() { return "parent static"; }
String describe() { return "parent method"; }
}
static class Child extends Parent {
String name = "child field";
static String kind() { return "child static"; }
@Override
String describe() { return "child method"; }
}
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.describe()); // instance method: object's type
System.out.println(p.name); // field: variable's type
System.out.println(p.kind()); // static method: variable's type
}
}Output
child method parent field parent static
22. What is upcasting and downcasting?
Upcasting is treating a child as its parent type (Animal a = new Dog();), always safe and automatic. Downcasting goes back (Dog d = (Dog) a;) and needs a cast; if the object is not really a Dog, it throws ClassCastException. Check first with instanceof.
Abstraction and interfaces
23. What is abstraction?
Showing what an object does and hiding how. You call list.add(x) without knowing how the list stores it. In Java you express abstraction with abstract classes and interfaces.
24. What is the difference between an abstract class and an interface?
An abstract class can hold state (fields), constructors and finished methods, and a class extends only one. An interface describes a capability, has no instance fields, and a class can implement many. Use an abstract class for a shared base with common state, an interface for a role that unrelated classes can play.
25. Can an abstract class have a constructor?
Yes. You cannot call new on it directly, but its constructor runs when a subclass object is created, to set up the fields the abstract class declares.
26. What are default and static methods in interfaces?
Since Java 8, an interface can have default methods (with a body, inherited by implementing classes and overridable) and static methods (helpers called on the interface name). Default methods let an interface grow without breaking existing classes.
27. What is a functional interface?
An interface with exactly one abstract method, such as Runnable, Comparator or Function. You can implement it with a lambda. @FunctionalInterface makes the compiler check the rule.
28. What is a marker interface?
An interface with no methods that only tags a class, such as Serializable or Cloneable. Today annotations usually do this job, but the old marker interfaces are still asked about.
The Object class and equality
29. Which methods of Object should you know?
equals(), hashCode(), toString(), getClass(), plus clone(), wait(), notify() and notifyAll(). Interviewers mostly ask about the first three.
30. What is the contract between equals() and hashCode()?
If two objects are equal by equals(), they must return the same hashCode(). The opposite is not required: different objects may share a hash code. Break the rule and HashMap and HashSet stop finding your objects.
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class EqualsHashCode {
static class Point {
final int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point)) return false;
Point p = (Point) o;
return x == p.x && y == p.y;
}
@Override
public int hashCode() { return Objects.hash(x, y); }
}
public static void main(String[] args) {
Set<Point> points = new HashSet<>();
points.add(new Point(1, 2));
points.add(new Point(1, 2)); // equal: not added again
System.out.println(points.size());
System.out.println(points.contains(new Point(1, 2)));
}
}Output
1 true
31. What does the default toString() print?
The class name, an @ and the hash code in hex, such as Student@1b6d3586. Override it to print something useful for logs and debugging; records do this for you.
32. What is the difference between a shallow copy and a deep copy?
A shallow copy copies the fields, so both objects share the same inner objects such as lists. A deep copy also copies those inner objects, so changing one never affects the other. Copy constructors are the clearest way to do either.
How to answer OOP questions well
- Give a one-line definition, then a real example from a project or from everyday life.
- Name the Java keyword involved (
extends,implements,private,@Override). - For output questions, decide first whether the member is an instance method (object's type) or a field or static method (variable's type).
- If you mention a design idea such as composition over inheritance, be ready to say why.
Frequently asked questions
Which OOP topics are asked most in Java interviews?
The four pillars, overloading versus overriding, abstract class versus interface, equals() and hashCode(), and output questions on polymorphism.
Should I memorise definitions for OOP interviews?
Know a one-line definition, but spend more time on examples and small programs. Interviewers check understanding with follow-up questions, not with the definition.
Is Java a pure object-oriented language?
No. It has primitive types such as int and boolean that are not objects, and static members that belong to no object.
Learn it step by step
Practise this, step by step
Short lessons with quizzes, practice and interview questions, from your first program to job-ready.