08/08/2025 1
A Presentationon Method Overloading
and Method Overriding
Presented By
Md. Rayhanul Islam
Institute of Information Technology
University of Dhaka
2.
08/08/2025 2
Outline
What isMethod?
Parts of Method
Method Overloading
Example of Method Overloading
Method Overriding
Example of Method Overriding
Rules for Method Overriding
3.
08/08/2025 3
What isMethod?
A collection of statements that are grouped together to perform an operation
A method has the following syntax:
modifier return_Value_Type Method_Name(list of parameters){
// Method body;
}
4.
08/08/2025 4
Parts ofMethod
1. Modifiers:
Tells the compiler how to call the method
Defines the access type of the method
2. Return Types
The data type of the value the method returns
The return type void when no value is returned
5.
08/08/2025 5
Parts ofMethod (Cont…)
Method Name
The actual name of the method
Method name and the parameter list together constitute the method signature
Parameters
When a method is invoked, a value is passed to parameter
This value is referred to as actual parameter or argument
The parameter list refers to the type, order, and number of the parameters
6.
08/08/2025 6
Parts ofMethod (Cont…)
Method Body
The method body contains a collection of statements that define what the method does
7.
08/08/2025 7
Method Overloading
Methodwith same name but different behavior
Method with same but different method signature
Overloaded methods may or may not have different return type but it should have different
argument(s)
int test (){ }
int test(int a){ }
int test(double a){ }
int test (double a, int a){ }
8.
08/08/2025 8
Code Example
publicclass MethodOverloading {
// Method 1
public int Addition(int a, int b) {
System.out.println(“Method 1 is called”);
return a + b;
}
// Method 2
public int Addition(int a, int b, int c) {
System.out.println(“Method 2 is called”);
return a + b + c;
}
}
9.
08/08/2025 9
Code Example(Cont…)
public static void main( String[]args ) {
int Answer1 = Addition(5, 6);
// In this case Method 1 will be called
System.out.println(“Answer 1 = ” + Answer1);
System.out.println(“----------------------------”);
int Answer2 = Addition(5, 6, 7);
// In this case Method 2 will be called
System.out.println(“Answer 2 = ” + Answer2);
}
10.
08/08/2025 10
Output
Output ofthe code
Method 1 is called
Answer 1 = 11
----------------------------
Method 2 is called
Answer 2 = 18
11.
08/08/2025 11
Method Overriding
The child class provides alternative implementation for parent class method.
The key benefit of overriding is the ability to define behavior that's specific to a particular
subclass type.
Overridden method: In the superclass.
Overriding method: In the subclass.
12.
08/08/2025 12
Example ofMethod Overriding
public class Car {
public void maxSpeed(){
System.out.println("Max speed is 60 mph");
}
}
class Ferrari extends Car{
public void maxSpeed() {
System.out.println("Max speed is 120 mph");
}
public void msc(){}
}
13.
08/08/2025 13
Method Overriding
public class TestCar
{
public static void main(String args[]){
Ferrari f=new Ferrari();
f.maxSpeed();
}
OR
public static void main(String args[]){
Car f=new Ferrari();
f.maxSpeed();
}
}
14.
08/08/2025 14
Rules forMethod Overriding
EXACT argument list matching
public class Car{
public void maxSpeed(int speed){
System.out.println("Max speed is ” +speed+ “ mph");
}
}
class Ferrari extends Car{
public void maxSpeed(float speed){
System.out.println("Max speed is ” +speed+ “ mph");
}
public void msc(){}
}
15.
08/08/2025 15
Rules forMethod Overriding
The return type must be the same as, or subtype of the return type declared in the
parent class
class Alpha {
Alpha doStuff(char c) {
return new Alpha();
}
}
class Beta extends Alpha {
Beta doStuff(char c) {
return new Beta();
}
}
16.
08/08/2025 16
Rules forMethod Overriding
Possible only through Inheritance
public class Car {
protected void maxSpeed(){
System.out.println("Max speed is 60 mph");
}
}
class Ferrari {
public void maxSpeed(){
System.out.println("Max speed is 120 mph");
}
public void msc(){}
}
17.
08/08/2025 17
Thank you
Althoughthese two terms sound similar but distinct in meaning.
For more information:
The Complete Reference by Herbert Schildt
Starting out with C++ by Tony Gaddis