Java Interview Notes (Up to Exception Handling)
1. OOP & Inheritance
Child classes can have additional methods not present in parent.
A a = new B(); is valid (upcasting). Access depends on reference type, execution depends on
object.
Method overriding: same signature, runtime polymorphism.
Method overloading: same name, different parameters.
Final method cannot be overridden.
2. Static vs Non-static
Static methods belong to class, resolved at compile time.
Non-static methods belong to object, resolved at runtime.
Static methods cannot be overridden, only hidden.
3. JVM & Memory
Java code -> compiled to bytecode (.class).
JVM loads classes, verifies, links, and executes.
Objects are stored in heap, references in stack.
Class loading is lazy (on first use).
4. Strings
String is immutable.
String Constant Pool (SCP) avoids duplicate literals.
new String() creates object in heap.
StringBuilder: mutable, not thread-safe.
StringBuffer: mutable, thread-safe.
5. equals() and hashCode()
Default equals() compares references.
Override equals() for logical equality.
hashCode() used in hashing structures.
Contract: if equals() is true, hashCode() must be same.
6. Exception Handling
Exception disrupts normal flow.
Hierarchy: Throwable -> Exception / Error.
Checked exceptions (compile-time), Runtime exceptions (runtime).
try-catch-finally used to handle exceptions.
finally always executes unless JVM exits.
throw: explicitly throw exception.
throws: declare exception in method signature.