Java Day-45
Method Overloading
✔ Definition
Method Overloading means the same method name with different parameters in the same class or parent–child classes.
✔ Why Method Overloading?
- To increase readability
- To perform the same action for different data types
- Compile-time polymorphism
Access Modifiers and Overloading
Yes, access modifiers can be changed in method overloading because:
- Overloading depends only on the method signature (name + parameters)
- Return type and access modifiers do NOT matter
Type Promotion in Method Overloading
Java automatically promotes smaller types to larger ones when no matching method exists.
Promotion order
a) char → int → long → float → double
b) byte → short → int → long → float → double
Example:
class Alpha {
public void accept(int x) { System.out.println("Integer type"); }
public void accept(long x) { System.out.println("Long type"); }
}class Beta extends Alpha {
public void accept(float x) { System.out.println("Float type"); }
void accept(double x) { System.out.println("Double type"); }
}public class MOLDemo3 {
public static void main(String[] args) {
Beta b = new Beta();
b.accept('A'); // char → int → long → float...
}
}
2. Var-Args (Variable Arguments)
✔ Introduced in JDK 1.5
✔ Meaning
Var-args allow 0 to N arguments in a method using ....
Get Aman Verma’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
public void input(int... x) {
System.out.println("Executed!!!");
}Rules of Var-Args
- Only one var-arg allowed per method
- Must be the last parameter
- Can be used in a method or a constructor
Example 1: Var-args accepts 0 to n arguments
class Test {
public void input(int... x) {
System.out.println("Executed!!!");
}
}Usage:
t1.input();
t1.input(5);
t1.input(5,10);
t1.input(5,10,15);Example 2: Var-args can accept heterogeneous data
class Hetro {
public void acceptHetro(Object... x) {
for (Object y : x) {
System.out.println(y);
}
}
}Usage:
new Hetro().acceptHetro(1, 2.3, 'A', false, "NIT", new StringBuilder("Java"));Example 3: Sum of parameters using var-args
class Addition {
public void sumOfParameter(int... values) {
int sum = 0;
for (int value : values) {
sum += value;
}
System.out.println("Sum =" + sum);
}
}3. Invalid Var-Args Examples
The following are illegal:
public void accept(float... x, int... y) ❌
public void accept(int... x, int y) ❌
public void accept(int x, int... y, int z) ❌4. Method Overriding in Java
✔ Overriding = redefining a parent class method in a subclass
✔ Only for non-static methods
(dynamic polymorphism)
❌ Static methods cannot be overridden
(static polymorphism → method hiding)
Rules for Method Overriding
- Same method name
- Same parameters
- Return type must be the same or covariant
- An access modifier cannot be reduced
- Only instance methods can be overridden
- Inheritance is required
Co-Variant Return Type
From Java 1.5
Subclass may change the return type to its child class.
Example:
class Super {
Animal m1() { return new Animal(); }
}class Sub extends Super {
Dog m1() { return new Dog(); } // Valid (Dog is subclass of Animal)
}5. Advantage of Method Overriding
✔ To change the implementation of the parent method in the child class
✔ To achieve runtime polymorphism
✔ To provide specific behavior for each subclass
Example:
class Animal {
public void eat() {
System.out.println("Generic Eating");
}
}class Dog extends Animal {
public void eat() {
System.out.println("Non Veg type");
}
}class Horse extends Animal {
public void eat() {
System.out.println("Veg type");
}
}6. Method Hiding
- Applies only to static methods
- Looks like overriding, but resolved at compile time









