Experiment 2.
Student Name:Akshit Rawat UID: 20BCS7246
Branch: CSE Section/Group 31-B
Semester: 3rd Date of Performance :14-Oct
Subject Name - JAVA LAB Subject Code: 21O-20CSP-219
1. Aim/Overview of the practical:
program to differentiate between method overloading and
method overriding.
2. Task to be done/ Which logistics used:
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.
3. Steps for experiment/practical/Code:
package [Link];
import [Link];
public class exp5 {
//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;
}
}
public static void main (String[] args){
// Overloading example
Sum obj = new Sum();
[Link]("***************************20BCS7252********************
********");
[Link]("Example of Overloading ");
[Link]("---------------------");
[Link]("Sum of two numbers: "+[Link](40, 78));
[Link]("Sum of three numbers: "+[Link](88, 67,
50));
[Link]("Sum of four numbers: "+[Link](12, 99, 89,
98));
[Link]("Sum of five numbers: "+[Link](9, 8, 4, 5,
2));
// Overriding example
CarClass obj2 = new Ford();
int num= [Link]();
[Link]("***************************20BCS7252********************
********");
[Link]("Example of overriding ");
[Link]("---------------------");
[Link]("Speed Limit is: "+num);
}
}
4. Observations/Discussions/ Complexity Analysis:
a. When a class has more than one method having the same name but
different in parameters, it is called method overloading in Java. b. When
the method of superclass is overridden in subclass to provide more
specific implementation, it is called method overriding in Java.
5. Result/Output/Writing Summary:
6.
Learning outcomes (What I have learnt):
1 Method overloading is used to increase the readability of the program.
2. Method overriding occurs in two classes that have IS-A (inheritance)
relationship.
3. In case of method overloading, parameter must be different.
4. Return type must be same or covariant in method overriding.
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.