0% found this document useful (0 votes)
54 views

Experiment 2.1: Program To Differentiate Between Method Overloading and Method Overriding

The document discusses method overloading and overriding in Java. It provides examples to differentiate between the two concepts. Method overloading has the same name but different parameters, while overriding has the same name and parameters but provides a more specific implementation in the subclass.

Uploaded by

Akshit Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Experiment 2.1: Program To Differentiate Between Method Overloading and Method Overriding

The document discusses method overloading and overriding in Java. It provides examples to differentiate between the two concepts. Method overloading has the same name but different parameters, while overriding has the same name and parameters but provides a more specific implementation in the subclass.

Uploaded by

Akshit Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 com.company;
import java.util.Scanner;

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();

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

CarClass obj2 = new Ford();


int num= obj2.speedLimit();

System.out.println("***************************20BCS7252********************
********");
System.out.println("Example of overriding ");
System.out.println("---------------------");
System.out.println("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.

You might also like