Java_Practice_Questions
Java_Practice_Questions
/* 2. Method Overloading */
// Method Overloading allows multiple methods with the same name but different parameters.
// It improves code readability and reusability.
class OverloadDemo {
void show(int a) {
System.out.println("Integer: " + a);
}
void show(double b) {
System.out.println("Double: " + b);
}
}
/* 3. Recursion */
// Recursion is a process where a function calls itself to solve a problem in a smaller instance.
// Example: Factorial calculation using recursion.
class RecursionExample {
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
}
/* 5. Access Specifiers */
// Access specifiers define the scope and visibility of class members.
// Types:
// - private: Accessible within the class only.
// - protected: Accessible within the package and subclasses.
// - public: Accessible from anywhere.
class AccessSpecifiers {
private int a = 10;
protected int b = 20;
public int c = 30;
}