Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
A method is like a recipe. You name it "Make Coffee", define the steps once, and call it whenever you want coffee. No need to repeat steps every time.
Anatomy of a Method
java
QUBITS OF DPK
Types of Methods
void Method
java
QUBITS OF DPK
Return Type Method
java
QUBITS OF DPK
Static Method
java
QUBITS OF DPK
Method Overloading
Same name, different parameters — resolved at compile time.
java
QUBITS OF DPK
Valid vs Invalid Overloading
java
QUBITS OF DPK
Stack Memory for Methods
javascript
QUBITS OF DPK
Pass By Value — Java's Golden Rule
java
QUBITS OF DPK
Java is always pass-by-value. For objects, the value IS the reference.
Recursion
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"A method is a named reusable code block. Java methods have return type, name, parameters, and body. Overloading allows same name with different parameters — resolved at compile time (compile-time polymorphism). Java is always pass-by-value — primitives pass a copy, objects pass a copy of the reference so the object itself can be modified. Every method call creates a stack frame destroyed on return. Recursion needs a mandatory base case to prevent StackOverflowError."
Interview Questions & MAANG-Level Answers
Q1. What is method overloading? How is it different from overriding?
Overloading: same method name, different parameter list (type/count/order), in the SAME class — resolved at compile time (static polymorphism). Overriding: child class redefines parent's method with SAME name and SAME parameters — resolved at runtime (dynamic polymorphism). Return type alone cannot create overloading. @Override annotation should always be used for overriding to catch typos at compile time.
java
QUBITS OF DPK
Q2. Is Java pass-by-value or pass-by-reference?
Java is ALWAYS pass-by-value — but the value being passed for objects IS the reference (address). For primitives: a copy of the value is passed — changes inside method don't affect original. For objects: a copy of the reference is passed — both caller and callee point to same heap object, so field modifications are visible, but reassigning the reference inside the method doesn't affect the original. There is no true pass-by-reference in Java.
Q3. What is a stack frame?
Every method call creates a stack frame pushed onto the call stack. The frame contains: local variables, method parameters, return address, and intermediate results. When the method returns, its frame is popped and destroyed — local variables are gone. This is why local variables can't be accessed after method returns. Stack overflow occurs when too many frames are pushed (deep recursion without base case).
Q4. What is the difference between void and a return type method?
void methods perform an action but return nothing — used for operations like printing, updating state, sending notifications. Return type methods compute and return a value — caller can use the result. void methods can have an empty return; to exit early (not to return a value). You cannot assign the result of a void method to a variable.
Q5. Why must recursive methods have a base case?
Without a base case, the method calls itself infinitely, creating new stack frames continuously until the JVM's call stack overflows, throwing StackOverflowError. The base case is the termination condition that stops recursion. Example: factorial(0) return 1 is the base case that stops factorial(n) = n * factorial(n-1). Every recursive problem must have at least one base case and each recursive call must move toward it.