Category: java
method override and overload
Published on 14 Feb 2026
Explanation
Method Overloading
same method name but different parameters in the same class.
We can change
Number of parameters
Type of parameters
Order of parameters
Code:
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b){
return a + b;
}
}
//next slide
Explanation
Coding
Code:
public class Main {
public static void main(String[] args) {
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
System.out.println(obj.add(5.5, 2.5));
}
}
}
Explanation
Method Overriding
Run-Time Polymorphism
IT means child class provides its own implementation
of parent class method.
Rules:
1.Method name must be same
2.Parameters must be same
3.Inheritance required
Code:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
Explanation
Cont..
Code:
public class Main {
public static void main(String[] args) {
Animal obj = new Dog();
obj.sound();
}
}