OOPS in Java – Interview Notes & Cheat Sheet
■ OOPS Concepts (Summary)
- **Data Hiding** – Secure internal data using `private`.
- **Abstraction** – Show *what*, hide *how* (via abstract classes & interfaces).
- **Encapsulation** – Binding data + methods (Encapsulation = Data Hiding + Abstraction).
- **Inheritance (IS-A)** – Reusability using `extends`. Multiple inheritance supported only via interfaces.
- **HAS-A** – Composition/Aggregation (Car HAS-A Engine).
- **Polymorphism** – Overloading (Compile-time) & Overriding (Runtime).
- **Constructors** – Used for initialization, can be overloaded, not inherited.
- **Coupling & Cohesion** – Loose coupling & High cohesion = Best practice.
- **Type Casting** – Converting references (compile-time & runtime rules).
- **Singleton & Factory** – Restrict/control object creation.
- **Control Flow** – Static (class load), Instance (every object creation).
■ Core Interview Questions & Answers
Question Answer
Difference between Data Hiding & Abstraction? Data Hiding → Restrict data access using private. Abstraction → Show only services via abstract classe
What is Encapsulation? Wrapping data + methods into a single class. Advantage → Security, maintainability.
Why no multiple inheritance in Java classes? To avoid ambiguity (diamond problem). Supported via interfaces.
What are Covariant Return Types? Since Java 1.5 – Child can override parent method with more specific return type.
Constructor vs Instance Block? Order: Instance block runs before constructor. Constructor can take args; block cannot.
Overloading vs Overriding? Overloading → Compile-time. Overriding → Runtime. Return types differ.
ArrayList vs List reference? ArrayList ref can call both ArrayList + List methods. List ref → only List methods.
Ways to create object in Java? new, Reflection, clone, Factory method, Deserialization.
Singleton class? A class that allows only one object using private constructor + static instance.
■ Scenario-Based Questions (with Answers)
Q1. Overloading: If `void m1(int)` and `void m1(float)` exist, calling `m1('a')` → int method.
Q2. Overriding: Parent public → Child private → Compile error (cannot reduce visibility).
Q3. Checked Exceptions: If parent method doesn’t throw, child cannot throw IOException.
Q4. Method Hiding: Parent & Child static → Reference type decides.
Q5. Parent constructor throws IOException → Child must declare same or parent exception.
Q6. Object o = new String("hi"); o.length() ■ → Must cast: ((String)o).length().
Q7. Abstract class CAN have constructor – runs during child object creation.
Q8. Without main() – Possible until Java 6 using static block. From Java 7 → main() mandatory.
■ Java Versions (Up to Java 23)
Version Release Date Key Features
Java 5 2004 Generics, annotations, enums, varargs, for-each
Java 6 2006 Performance & scripting improvements
Java 7 2011 try-with-resources, diamond operator, NIO.2
Java 8 (LTS) 2014 Lambdas, Streams, Date/Time API, default methods
Java 9 2017 Modules (JPMS), JShell
Java 10 2018 var keyword
Java 11 (LTS) 2018 HTTP client, String API improvements
Java 12 2019 Switch expressions (preview)
Java 13 2019 Text blocks (preview)
Java 14 2020 Records, pattern matching (preview)
Java 15 2020 Sealed classes, hidden classes
Java 16 2021 Records (final), instanceof matching
Java 17 (LTS) 2021 Sealed classes (final), new macOS rendering
Java 18 2022 Simple web server (incubator)
Java 19 2022 Virtual threads (preview)
Java 20 2023 Scoped values, virtual threads (preview)
Java 21 (LTS) 2023 Virtual threads, Record patterns, unnamed classes
Java 22 2024 Statements before super(), string templates (preview)
Java 23 2024 Scoped values, structured concurrency, module import declarations