Java Core Concepts – Interview Cheat Sheet
OOPs Principles
- Definition: Organizes code into classes/objects.
- Key: Encapsulation, Abstraction, Inheritance, Polymorphism.
- Example: BankAccount (encapsulation), ATM (abstraction), SavingsAccount extends
BankAccount (inheritance), calculateInterest() (polymorphism).
Class and Object
- Definition: Class = blueprint, Object = instance.
- Key: Multiple objects from one class, class defines attributes + behavior.
- Example: Student class → s1 = new Student().
Constructors
- Definition: Special method to initialize objects.
- Key: Same name as class, no return type. Types: default, no-arg, parameterized, copy.
Static Members
- Definition: Belong to class, not object.
- Key: Memory efficient, accessed via class name, used for counters/utilities.
- Example: [Link]().
Garbage Collection
- Definition: Automatic memory management by JVM.
- Key: Removes unused objects, calls finalize() before destruction.
this vs super
- this → current object.
- super → parent class.
- Example: [Link]() calls parent’s method.
final vs finally vs finalize()
- final → keyword (constant, no override, no inherit).
- finally → block always executes in exception handling.
- finalize() → method called before GC destroys object.
== vs equals()
- == → compares reference (memory).
- equals() → compares content.
- Example: String a = new String('hi'); a==b? false; [Link](b)? true.
Java vs Python / C++ / C
- Java vs Python → Java: static, fast; Python: dynamic, slow, AI-friendly.
- Java vs C++ → Java: auto GC, no multiple inheritance; C++: manual memory, multiple
inheritance.
- C vs C++ → C: procedural; C++: OOP.
Access Specifiers
- public → everywhere.
- protected → package + subclass.
- default → package only.
- private → same class.
Inheritance
- Definition: Acquire parent class features.
- Types: Single, Multilevel, Hierarchical.
- Note: Multiple inheritance not supported (diamond problem). Achieved via interfaces.
Polymorphism
- Compile-time → overloading.
- Run-time → overriding.
Run-time Polymorphism
- Definition: Parent ref → child object executes child method.
- Key: Achieved using method overriding.
Abstract Class
- Definition: Class with at least one abstract method.
- Key: Cannot instantiate, can contain abstract + normal methods.
- Example: Shape class with abstract draw().
Multiple Inheritance
- Java doesn’t support with classes → diamond problem.
- Achieved via interfaces.
Overloading vs Overriding
- Overloading → same name, diff params, compile-time.
- Overriding → same name/signature, child class, run-time.
Exception Handling
- Definition: Handles runtime errors gracefully.
- Key: try, catch, finally, throw, throws.
- Example: try { int a=10/0; } catch(ArithmeticException e) {}
Multithreading
- Definition: Concurrent execution of tasks.
- Key: Extend Thread / Implement Runnable, methods: start(), run(), sleep(), join().
- Example: Banking & Printing Passbook simultaneously.