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

Java Exp5 C28

The document discusses method overloading in Java. It provides examples of overloading methods by changing the number and type of arguments. The key points are: 1) Method overloading increases readability by allowing multiple methods with the same name but different signatures. 2) There are two ways to overload methods in Java - by changing the number of arguments or by changing the data type of arguments. 3) Overloading is not possible by only changing the return type, as this causes ambiguity for the compiler.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Java Exp5 C28

The document discusses method overloading in Java. It provides examples of overloading methods by changing the number and type of arguments. The key points are: 1) Method overloading increases readability by allowing multiple methods with the same name but different signatures. 2) There are two ways to overload methods in Java - by changing the number of arguments or by changing the data type of arguments. 3) Overloading is not possible by only changing the return type, as this causes ambiguity for the compiler.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PART A

(PART A: TO BE REFFERED BY STUDENTS)


Experiment No. 05
Aim: Write a program to calculate the area of different shapes (circle, triangle, rectangle,
square) by using method overloading
Objective: To create a class with methods using different no of parameters and their
overloading in real time scenario.
Outcome: Students successfully created and used method overloading as per requirement of
the real time scenario
Theory:

Method Overloading
If a class has multiple methods by same name but different parameters, it is known as Method
Overloading. If we have to perform only one operation, having same name of the methods
increases the readability of the program. Suppose you have to perform addition of the given
numbers but there can be any number of arguments, if you write the method such as a(int,int)
for two parameters, and b (int,int,int) for three parameters then it may be difficult for you as
well as other programmers to understand the behavior of the method because its name differs.
So, we perform method overloading to figure out the program quickly.
Advantage of method overloading?
Method overloading increases the readability of the program.

Different ways to overload the method


There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
1) Method Overloading: changing no. of arguments
In this example, two methods are created, first add() method performs addition of two
numbers and second add method performs addition of three numbers.

In this example, static methods are created so that don't need to create instance for calling
methods.

1. class Adder{
2. static int add(int a,int b){return a+b;}
3. static int add(int a,int b,int c){return a+b+c;}
4. }
5. class TestOverloading1{
6. public static void main(String[] args){
7. System.out.println(Adder.add(11,11));
8. System.out.println(Adder.add(11,11,11));
9. }}

Output :
22
33

2) Method Overloading: changing data type of arguments

In this example, two methods are created that differs in data type. The first add method
receives two integer arguments and second add method receives two double arguments.

1. class Adder{
2. static int add(int a, int b){return a+b;}
3. static double add(double a, double b){return a+b;}
4. }
5. class TestOverloading2{
6. public static void main(String[] args){
7. System.out.println(Adder.add(11,11));
8. System.out.println(Adder.add(12.3,12.6));
9. }}

Output :

22
24.9
Method Overloading is not possible by changing the return type of method only?

In java, method overloading is not possible by changing the return type of the method only
because of ambiguity. Let's see how ambiguity may occur:

1. class Adder{
2. static int add(int a,int b){return a+b;}
3. static double add(int a,int b){return a+b;}
4. }
5. class TestOverloading3{
6. public static void main(String[] args){
7. System.out.println(Adder.add(11,11));//ambiguity
8. }}

Output :
Compile Time Error: method add(int,int) is already defined in class Adder
Algorithm:-
1. Start
2. Define class Shape with its attributes and methods area() with different type and number
of parameters.
3. Define class Demo
4. Create object of Shape class inside class Demo.
5. Call different overloaded area() methods using object of Shape.
6. Stop
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded at the end of the practical)
Roll. No. C28 Name: Prathmesh Krishna Gaikwad
Class: SE-C Batch: B-1
Date of Experiment: 28/10/2021 Date of Submission: 05/11/2021
Expriement No.:05 Grade:

B.1 Software Code written by student:


import java.util.*;
class Shape
{
void area(int a)
{
System.out.println("the area of the square is "+ a*a +" sq units");
}
void area(float l,float b)
{
System.out.println("the area of the rectangle is "+ l*b +"sq units");
}
void area(double r)
{
double z = 3.14 * r * r;
System.out.println("the area of the circle is "+ z +"sq units");
}
void area(double b,double h)
{
double m = 0.5 * b * h;
System.out.println("the area of the circle is "+ m +"sq units");
}

}
class Demo
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
Shape t = new Shape();
System.out.print("Enter the side of Square=");
int a = s.nextInt();
t.area(a);
System.out.println();
System.out.print("Enter the length & breadth of Rectangle=");
float l = s.nextFloat();
float b = s.nextFloat();
t.area(l,b);
System.out.println();
System.out.print("Enter the radius of Circle=");
double r = s.nextDouble();
t.area(r);
System.out.println();
System.out.print("Enter the base & height of Triangle=");
double p = s.nextDouble();
double h = s.nextDouble();
t.area(p,h);
System.out.println();
}
}
B.2 Input and Output:

B.3 Observations and learning:


Ans- One can overload constructor to perform different tasks.
Different type of overload methods in java on which changing the number of argument
and changing the numbers of data.
Just like we know that different objects have different techniques of calculating area.
Method overloading help us to use the same identifier.

B.4 Conclusion:
Ans- Method overloading is helpful to access to users. The function names need not be different
for similar purposes, only the argument can be changed.

B.5 Question of Curiosity

1.What is method overloading?

Ans-

A)If a class contains multiple methods with the same name,then it is called as method
overloading.

B)Although name of methods are same but theior parameter list need to be different.It
means either number of parameter or type of parameter or both should be different.
2.What is method signature? What are the things it consist of?

Ans- In Java, a method signature is composed of a name and the number, type and
order of its parameters. Return types and thrown exceptions are not considered to be a
part of the method signature, nor are the names of parameters; they are ignored by the
compiler for checking method uniqueness.

3.Can we declare one overloaded method as static and another one as non-static?

Ans- Yes, we can declare an overloaded method as static and another one as non-
static.

4.How do compiler differentiate overloaded methods from duplicate methods?

Ans- Compiler uses method signature to check whether the method is overloaded or
duplicated. Duplicate methods will have same method signatures i.e. same name, same
number of arguments and same types of arguments. Overloaded methods will also have same
name but differ in number of arguments or else types of arguments.

5.Is it possible to have two methods in a class with same method signature but different
return types?

Ans- We can not define more than one method with the same name, Order and the type
of the arguments. It would be a compiler error. The compiler does not consider the
return type while differentiating the overloaded method. But we cannot declare
two methods with the same signature and different return type.

6.In “MyClass” , there is a method called “myMethod” with four different overloaded
forms. All four different forms have different visibility ( private, protected, public and
default). Is “myMethod” properly overloaded?

Ans: Yes. Compiler checks only method signature for overloading of methods not the
visibility of methods.

7.Can overloaded methods be synchronized?


Ans- Yes, overloaded methods can be synchronized in java.

8.Can we overload main() method?

Ans- we can overload main() method. A class can have any number of main()
methods but execution starts from public static void main(String[] args) only.
9.Can we declare overloaded methods as final?

Ans- Yes, we can declare overloaded methods as final.

10.Overloading is the best example of dynamic binding. True or false?

Ans- False. Overloading is the best example for static binding.

You might also like