OOP Interview Questions

What are the four pillars of OOP?

PillarMeaningJava feature
Encapsulationhide data, expose behaviourprivate fields + public methods
Inheritancereuse and extend a parent typeextends, implements
Polymorphismone interface, many implementationsoverloading, overriding
Abstractionshow what, hide howabstract classes, interfaces

What is the difference between method overloading and overriding?

OverloadingOverriding
Wheresame classsubclass
Signaturesame name, different parameterssame name and parameters
Resolved atcompile time (static polymorphism)runtime (dynamic dispatch)
Return typecan differsame or covariant (subtype)
static / private methodscan be overloadedcannot be overridden (static ones are hidden)

Show runtime polymorphism with an example.

Example

The reference type is Payment, but the method of the actual object runs.

import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Payment> payments = List.of(new UpiPayment(), new CardPayment());
        for (Payment p : payments) {
            p.pay(500);
        }
    }
}

abstract class Payment {
    abstract void pay(int amount);
}

class UpiPayment extends Payment {
    void pay(int amount) { System.out.println("Paid Rs " + amount + " via UPI"); }
}

class CardPayment extends Payment {
    void pay(int amount) { System.out.println("Paid Rs " + amount + " via Card"); }
}

Output

Paid Rs 500 via UPI
Paid Rs 500 via Card

What is the difference between an abstract class and an interface?

Abstract classInterface
Inheritancea class extends only onea class implements many
Statecan have instance fieldsonly public static final constants
Constructorsyesno
Methodsabstract and concrete, any accessabstract, default, static, private (Java 9+)
Use whenrelated classes share state/code ("is-a")unrelated classes share a capability ("can-do")

Why does Java not support multiple inheritance of classes?

To avoid the diamond problem: if two parents define the same method, the child would not know which one to inherit. Java allows multiple inheritance of types through interfaces. If two interfaces give conflicting default methods, the class must override it and can pick one with A.super.method().

What is constructor chaining? Are constructors inherited?

Constructors are not inherited. Every constructor first calls a parent constructor (implicitly super()), so object creation runs from the top of the hierarchy down.

Example

Parent constructors run first.

public class Main {
    public static void main(String[] args) {
        new C();
    }
}

class A { A() { System.out.println("A constructor"); } }
class B extends A { B() { System.out.println("B constructor"); } }
class C extends B { C() { System.out.println("C constructor"); } }

Output

A constructor
B constructor
C constructor

What are access modifiers in Java?

ModifierClassPackageSubclass (other package)Everywhere
privateyesnonono
default (none)yesyesnono
protectedyesyesyesno
publicyesyesyesyes