Careeroza
OOP Interview Questions
What are the four pillars of OOP?
| Pillar | Meaning | Java feature |
|---|---|---|
| Encapsulation | hide data, expose behaviour | private fields + public methods |
| Inheritance | reuse and extend a parent type | extends, implements |
| Polymorphism | one interface, many implementations | overloading, overriding |
| Abstraction | show what, hide how | abstract classes, interfaces |
What is the difference between method overloading and overriding?
| Overloading | Overriding | |
|---|---|---|
| Where | same class | subclass |
| Signature | same name, different parameters | same name and parameters |
| Resolved at | compile time (static polymorphism) | runtime (dynamic dispatch) |
| Return type | can differ | same or covariant (subtype) |
| static / private methods | can be overloaded | cannot 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 class | Interface | |
|---|---|---|
| Inheritance | a class extends only one | a class implements many |
| State | can have instance fields | only public static final constants |
| Constructors | yes | no |
| Methods | abstract and concrete, any access | abstract, default, static, private (Java 9+) |
| Use when | related 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?
| Modifier | Class | Package | Subclass (other package) | Everywhere |
|---|---|---|---|---|
| private | yes | no | no | no |
| default (none) | yes | yes | no | no |
| protected | yes | yes | yes | no |
| public | yes | yes | yes | yes |