Autocoder

Java OOPs Interview Questions

Last updated 2026-11-19

Object-oriented programming in Java is tested through concrete behaviour: what does the constructor chain print, which method runs when a field is shadowed, why does an override not compile. This page works through the four pillars and the language rules around them, with the output of each snippet.

For language-neutral theory such as SOLID and design patterns, read OOPs interview questions. This page stays with how Java implements them.

Pillars coveredEncapsulation, inheritance, polymorphism, abstraction
Java version17 and later
LevelFresher to 3 years

Rules worth memorising first

Java OOPs Interview Questions: questions with worked answers

  1. 1

    Explain the four pillars of OOP with a Java example of each.

    easy

    Encapsulation hides state behind methods, for example a private balance field with deposit and withdraw that enforce rules. Inheritance lets a class reuse and extend another with extends. Polymorphism lets one reference type refer to many runtime types, so Shape s = new Circle() calls Circle.area(). Abstraction exposes what an object does and hides how, through abstract classes and interfaces. Interviewers usually follow up by asking which of these is compile-time and which is runtime polymorphism.

  2. 2

    What is the difference between compile-time and runtime polymorphism?

    easy

    Compile-time polymorphism is method overloading: the compiler picks the method from the argument types. Runtime polymorphism is method overriding: the JVM picks the method from the actual object type using the virtual method table. Operator overloading does not exist in Java, except that + is built in for String.

  3. 3

    What does this print? Trace the constructor chain.

    medium

    The child constructor implicitly calls super() first, and the parent constructor calls the overridable method before the child fields are initialised. So the child method sees the default value 0, not 5. This is why calling overridable methods from a constructor is a known bug source.

    java
    class Parent {
        Parent() { System.out.println("Parent ctor"); show(); }
        void show() { System.out.println("Parent show"); }
    }
    class Child extends Parent {
        int v = 5;
        Child() { System.out.println("Child ctor " + v); }
        @Override void show() { System.out.println("Child show " + v); }
    }
    new Child();
    // Parent ctor
    // Child show 0
    // Child ctor 5
  4. 4

    What is the difference between an abstract class and an interface in modern Java?

    medium

    An abstract class can hold state (instance fields), constructors and any visibility of methods, but a class extends only one. An interface has no instance state, only public constants, and a class can implement many. Since Java 8 interfaces can have default and static methods, and since Java 9 private methods, so the old rule that interfaces have no code no longer holds. Choose an abstract class for shared state and partial implementation; choose an interface for a capability contract. The dedicated page on interfaces and abstract classes goes deeper.

  5. 5

    What is method hiding, and what does this print for fields and static methods?

    medium

    A static method with the same signature in a subclass hides the parent one; it does not override it. Fields are never polymorphic, so the reference type decides. With Parent p = new Child(), p.name prints the parent field and p.greet() on a static method calls the parent version.

    java
    class Parent { String name = "parent"; static void greet() { System.out.println("P"); } }
    class Child extends Parent { String name = "child"; static void greet() { System.out.println("C"); } }
    Parent p = new Child();
    System.out.println(p.name); // parent
    p.greet();                  // P
  6. 6

    Can a constructor be private, abstract, final or static? Why would you make one private?

    medium

    A constructor cannot be abstract, final, static or inherited. It can be private, which blocks outside instantiation. That is the basis of singletons, static factory methods, utility classes that should never be created, and enum-like type-safe constants. A class whose constructors are all private cannot be subclassed from outside its own file or nest.

  7. 7

    What are the rules for overriding? Which of these compile?

    medium

    The overriding method needs the same name and parameter types, a return type that is the same or a subtype (covariant), no weaker access, and no broader checked exceptions. It cannot override a final, static or private method. Narrowing the return type compiles, widening the access compiles, but reducing public to protected does not and neither does throwing a new checked Exception that the parent method does not declare.

    java
    class A { public Number get() throws java.io.IOException { return 1; } }
    class B extends A {
        @Override public Integer get() { return 2; } // covariant return, fewer exceptions: OK
    }
    // protected Integer get() would fail: weaker access
    // public Integer get() throws Exception would fail: broader checked exception
  8. 8

    What does the super keyword do, and what is this()?

    easy

    super refers to the parent part of the current object. super(...) calls a parent constructor and super.method() calls the parent version of an overridden method. this refers to the current object; this(...) calls another constructor in the same class. Either constructor call must be the first statement, so you cannot use both in the same constructor.

  9. 9

    What is the diamond problem and how does Java handle it?

    medium

    Classes cannot extend two parents, which avoids state ambiguity. With interfaces, two default methods with the same signature create a conflict that the compiler forces you to resolve by overriding the method and optionally calling a specific one with InterfaceName.super.method().

    java
    interface X { default String hi() { return "X"; } }
    interface Y { default String hi() { return "Y"; } }
    class Z implements X, Y {
        public String hi() { return X.super.hi() + Y.super.hi(); } // XY
    }
  10. 10

    What is the difference between aggregation, composition and association?

    medium

    Association is any relationship where one class uses another. Aggregation is a has-a relationship in which the part can exist without the whole, such as a Department and its Teachers. Composition is a stronger has-a in which the part cannot outlive the whole, such as an Order and its OrderLines. In code the difference is mostly about who creates and owns the lifecycle of the part, not a language keyword.

  11. 11

    Why can a subclass instance be assigned to a parent reference, but calling a child-only method fails?

    hard

    The compiler checks calls against the declared (static) type. Parent p = new Child() is legal because Child is-a Parent, but p.childOnly() does not compile since Parent has no such method. A downcast ((Child) p).childOnly() compiles and is checked at runtime, throwing ClassCastException if the object is not actually a Child. Use instanceof pattern matching, introduced as final in Java 16, to cast safely.

    java
    Object o = "text";
    if (o instanceof String s && s.length() > 2) {
        System.out.println(s.toUpperCase()); // TEXT
    }
  12. 12

    What is encapsulation done well? Why not make fields public?

    medium

    Encapsulation means the class controls how its state changes, so invariants hold: a non-negative balance, a sorted list, a valid email. Private fields with validated setters or, better, immutable objects with no setters make that possible and let you change the internal representation later. Returning an internal mutable collection or array directly leaks the state, so return a copy or an unmodifiable view.

  13. 13

    What is the effect of declaring a class or method final, and how do you build an immutable class?

    medium

    A final class cannot be subclassed and a final method cannot be overridden. An immutable class needs: a final class (or private constructors), all fields private and final, no setters, defensive copies of mutable inputs and outputs, and no escaping of this from the constructor. String and the java.time classes follow this pattern. Records give shallow immutability with one line.

  14. 14

    What does this print? Overriding with a private method and with overloading.

    hard

    A private method is not inherited, so the child method of the same name is a brand new method and no override takes place. The call inside the parent invokes the parent version.

    java
    class P {
        private void who() { System.out.println("P"); }
        void call() { who(); }
    }
    class C extends P {
        void who() { System.out.println("C"); }
    }
    new C().call(); // P
  15. 15

    What is the difference between equals/hashCode contract and why must both be overridden?

    medium

    If two objects are equal according to equals(), they must return the same hashCode(). Hash-based collections find a bucket by hashCode first and then call equals() inside it, so overriding only equals() lets equal objects land in different buckets and a HashSet will hold duplicates. The reverse is allowed: unequal objects may share a hash code. The mechanics are covered in HashMap internals.

Frequently asked questions

What is the most asked OOP question in Java interviews?

Abstract class versus interface, followed by overloading versus overriding and the four pillars. Expect a snippet with constructors or static members as the follow-up.

Is Java fully object oriented?

No. Primitive types such as int and boolean are not objects, and static members exist outside any instance. Wrapper classes and autoboxing bridge the gap.

Can an interface extend a class?

No. An interface can only extend other interfaces, and a class can both extend one class and implement many interfaces.

What is the difference between this and super?

this refers to the current instance and super to its parent portion; both can be used to access members or call constructors.

Related guides

More in Programming Language Interview Questions by Language

See all in this section