Java Exp5 C28
Java Exp5 C28
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.
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
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:
}
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.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.
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.
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.
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?