What do you mean by Method Overloading in Java?
2024
What do you mean by Method Overloading in Java?
- A.
Having two methods with the same name in different packages.
- B.
Having two methods with the same name and same parameters.
- C.
Having two methods with the same name but different parameters.
- D.
Having two methods with the same name in different classes.
- E.
Question not attempted
Attempted by 115 students.
Show answer & explanation
Correct answer: C
Method Overloading in Java means defining multiple methods in the same class with the same method name but different parameter lists (different number, type, or order of parameters).
It is an example of compile-time polymorphism.
Example
class Demo {
void add(int a, int b) {
System.out.println(a + b);
}
void add(int a, int b, int c) {
System.out.println(a + b + c);
}
}Here, both methods are named add() but have different parameters, so this is Method Overloading.