Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 4 of 10
Methods - Design, Overloading & Recursion
Methods are the building blocks of Java programs. A well-designed method does exactly ONE thing, has a clear name, and is short enough to read at a glance. Method overloading, varargs, and recursion extend what methods can do. Getting method design right from day one saves endless debugging later.
Method Design Principles
- Single Responsibility - one method, one purpose. If you need 'and' in the name, split it.
- Clear naming - verb + noun: calculateTotal(), findBookById(), isValidEmail()
- Short methods - ideally < 20 lines. If longer, extract smaller methods.
- Return type: always declare what the method returns (void if nothing)
- Parameters: keep few (≤ 3 ideally). More = consider a parameter object.
- Access modifiers: public (any code), private (same class), protected (package + subclasses)
Methods, Overloading & Varargs
public class MethodDesign {
// Descriptive name: verb + noun, exactly one job
public static double calculateDiscountedPrice(double price, double discountPercent) {
if (price < 0 || discountPercent < 0 || discountPercent > 100) {
throw new IllegalArgumentException("Invalid price or discount");
}
return price * (1 - discountPercent / 100.0);
}
// Method overloading - same name, different parameters
// Java picks the right version based on argument types/count
public static double calculateTax(double amount) {
return amount * 0.18; // default 18% GST
}
public static double calculateTax(double amount, double taxRate) {
return amount * (taxRate / 100); // custom rate
}
// Varargs - variable number of arguments (becomes an array internally)
public static double sum(double... numbers) {
double total = 0;
for (double n : numbers) total += n;
return total;
}
// Recursion - method calls itself
// Classic: factorial n! = n × (n-1)!
public static long factorial(int n) {
if (n <= 1) return 1; // base case - stop condition
return n * factorial(n - 1); // recursive case
}
public static void main(String[] args) {
System.out.println(calculateDiscountedPrice(1000, 20)); // 800.0
System.out.println(calculateTax(500)); // 90.0
System.out.println(calculateTax(500, 12)); // 60.0
System.out.println(sum(10, 20, 30)); // 60.0
System.out.println(sum(5, 10)); // 15.0
System.out.println(factorial(5)); // 120
// factorial(5) = 5 × factorial(4) = 5 × 4 × 3 × 2 × 1 = 120
}
}Quick Quiz
Tip
Tip
Practice Methods Design Overloading Recursion in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Methods - Design, Overloading & Recursion
Practice Task
Note
Practice Task - (1) Write a working example of Methods Design Overloading Recursion from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Methods Design Overloading Recursion is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- Methods are the building blocks of Java programs.
- Single Responsibility - one method, one purpose. If you need 'and' in the name, split it.
- Clear naming - verb + noun: calculateTotal(), findBookById(), isValidEmail()
- Short methods - ideally < 20 lines. If longer, extract smaller methods.
Finished this lesson?