Menu
CoddyTech

OOPs in Java Interview Questions

The four pillars, and where fields and static methods behave differently from instance methods.

10 questions
  1. What are the four pillars of OOP in Java?

    Encapsulation (private fields behind methods), inheritance (extends to reuse a class), polymorphism (Animal a = new Dog(); a.sound() runs Dog's method) and abstraction (interfaces and abstract classes hide how things work).

    Back each with one example from your own code.

  2. What is the difference between method overloading and overriding?

    Overloading is one name with different parameter lists, chosen at compile time. Overriding is a subclass replacing an inherited method with the same signature, chosen at runtime.

    Java
    class Shape {
        double area() { return 0; }
    }
    
    class Circle extends Shape {
        double r;
        Circle(double r) { this.r = r; }
        @Override
        double area() { return Math.PI * r * r; }   // overriding
    }
    
    public class Main {
        static String describe(int n) { return "int " + n; }       // overloading
        static String describe(String s) { return "String " + s; }
    
        public static void main(String[] args) {
            Shape s = new Circle(1);
            System.out.printf("%.2f%n", s.area());
            System.out.println(describe(5) + ", " + describe("five"));
        }
    }

    It prints 3.14 and int 5, String five. Changing only the return type is not overloading and does not compile. See method overloading.

  3. Does creating a subclass object call the parent constructor?

    Java
    class Parent {
        Parent() { System.out.print("Parent "); }
    }
    
    class Child extends Parent {
        Child() { System.out.print("Child"); }
    }
    
    public class Main {
        public static void main(String[] args) {
            new Child();
            System.out.println();
        }
    }

    It prints Parent Child. A constructor first calls a superclass constructor; without an explicit super(...), the compiler inserts super().

    If Parent had only a constructor with parameters, Child would need super(args) to compile.

  4. What happens when a subclass redeclares a field and overrides a method?

    Java
    class Animal {
        String name = "animal";
        String sound() { return "..."; }
    }
    
    class Dog extends Animal {
        String name = "dog";
        @Override
        String sound() { return "woof"; }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal a = new Dog();
            System.out.println(a.name + " " + a.sound());
        }
    }

    It prints animal woof. Methods dispatch on the object's runtime class (Dog), but fields resolve at compile time from the reference type (Animal), so the subclass field only hides the parent's. Keep fields private.

  5. Can you override a static method in Java?

    Java
    class P {
        static String who() { return "P"; }
    }
    
    class C extends P {
        static String who() { return "C"; }
    }
    
    public class Main {
        public static void main(String[] args) {
            P p = new C();
            System.out.println(p.who());
        }
    }

    It prints P. Static methods are hidden, not overridden: the call binds at compile time to the declared type, so this is P.who(). Call static methods through the class name.

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

    Use an interface for a capability unrelated classes share, and an abstract class to share state and code among related ones.

    An abstract class can have instance fields and constructors, and a class extends only one. An interface holds only constants, but a class implements many, and since Java 8 interfaces can carry default and static methods. See abstract classes.

  7. Why does Java not support multiple inheritance of classes? How do default methods handle the diamond problem?

    To avoid the diamond problem: if two parent classes had the same method, the child's version would be ambiguous. Default methods can still clash between interfaces, so Java forces an override, which can pick one with Interface.super.method():

    Java
    interface Walker {
        default String move() { return "walk"; }
    }
    
    interface Swimmer {
        default String move() { return "swim"; }
    }
    
    class Duck implements Walker, Swimmer {
        @Override
        public String move() {             // required, or it does not compile
            return Walker.super.move() + " and " + Swimmer.super.move();
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            System.out.println(new Duck().move());
        }
    }

    It prints walk and swim. A method from a class always beats an interface default.

  8. What are the access modifiers in Java?

    From most to least open: public (everywhere), protected (package and subclasses), default with no keyword (package only) and private (the class only).

    A top-level class can only be public or package-private. More in access modifiers.

  9. What is composition and why is it often preferred over inheritance?

    Composition means a class holds another object and delegates to it ("has-a": a Car has an Engine) instead of extending it ("is-a").

    Prefer it because inheritance ties the child to the parent's implementation and exposes every inherited public method, as with Stack extends Vector in the JDK; a composed field can also be swapped at runtime. Keep inheritance for true subtypes.

  10. How do you create an immutable class in Java?

    Make the class final and its fields private final, add no setters, and copy mutable inputs and outputs.

    Java
    import java.util.ArrayList;
    import java.util.List;
    
    final class Team {
        private final String name;
        private final List<String> members;
    
        Team(String name, List<String> members) {
            this.name = name;
            this.members = List.copyOf(members);   // defensive, unmodifiable copy
        }
    
        List<String> members() { return members; }
    }
    
    public class Main {
        public static void main(String[] args) {
            List<String> input = new ArrayList<>(List.of("Asha", "Ben"));
            Team t = new Team("core", input);
            input.add("Eve");                     // does not affect the team
            System.out.println(t.members());
        }
    }

    It prints [Asha, Ben]; the defensive copy is what most candidates forget. A record (Java 16) covers the rest, but copy its mutable components in a compact constructor.

Coddy programming languages illustration

Learn to code with Coddy

Get started