Experiment 2.1: Program To Differentiate Between Method Overloading and Method Overriding
Experiment 2.1: Program To Differentiate Between Method Overloading and Method Overriding
In the method overloading, methods or functions must have the same name and
different signatures. Whereas in the method overriding, methods or functions
must have the same name and same signatures.
package com.company;
import java.util.Scanner;
//Overloading example
static class Sum {
int add(int n1, int n2) {
return n1 + n2;
}
int add(int n1, int n2, int n3) {
return n1 + n2 + n3;
}
int add(int n1, int n2, int n3, int n4) {
return n1 + n2 + n3 + n4;
}
int add(int n1, int n2, int n3, int n4, int n5) {
return n1 + n2 + n3 + n4 + n5;
}
}
// Overriding example
static class CarClass
{
public int speedLimit()
{
return 100;
}
}
static class Ford extends CarClass {
public int speedLimit() {
return 150;
}
}
System.out.println("***************************20BCS7252********************
********");
System.out.println("Example of Overloading ");
System.out.println("---------------------");
System.out.println("Sum of two numbers: "+obj.add(40, 78));
System.out.println("Sum of three numbers: "+obj.add(88, 67,
50));
System.out.println("Sum of four numbers: "+obj.add(12, 99, 89,
98));
System.out.println("Sum of five numbers: "+obj.add(9, 8, 4, 5,
2));
// Overriding example
System.out.println("***************************20BCS7252********************
********");
System.out.println("Example of overriding ");
System.out.println("---------------------");
System.out.println("Speed Limit is: "+num);
}
}
5. Result/Output/Writing Summary:
6.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
Sr. No. Parameters Marks Obtained Maximum Marks
1.
2.
3.