The document discusses method overloading in Java. Method overloading allows creating multiple methods with the same name but different parameters. It increases code readability. Methods can be overloaded by changing the number or type of arguments but not the return type. The method signature, which includes the number, type and order of arguments, must be different for overloaded methods. Constructors can also be overloaded in Java.
3. • If a class have multiple methods by same name but different parameters,
it’s known as method overloading
• It’s an example for polymorphism in java
• Method overloading increases the readability of a program
• There are two ways to overload a method in java
1)By changing number of arguments
2)By changing the data type
• Eg :System.out.println()
* In java method overloading is not possible by changing the return type of
the method
4. Properties of method overloading in Java
1) Overloaded methods are bonded using static binding in Java
2) Overloaded methods are fast because they are bonded during compile
time and no check or binding is required during runtime.
3)Most important rule of method overloading in Java is that two overloaded
method must have different signature.
method signature means in Java:
1) Number of argument to a method is part of method signature.
2) Type of argument to a method is also part of method signature
3) Order of argument also forms part of method signature provided they
are of different type.
4) return type of method is not part of method signature in Java.
5. Method overloading by changing the number of arguments
Class calculation
{
Void sum(int a, int b){
system.out.println(a+b); }
Void sum(int a,int b,int c){
system.out.println(a+b+c);}
Public static void main(string args[])
{
Calculation obj=new calculation();
Obj.sum(10,10);
Obj.sum(20,20,20)
}
6. Method overloading by changing data type of argument
Class calculation
{
Void sum(int a, int b)
{
system.out.println(a+b);
}
Void sum(double a,double b)
{system.out.println(a+b);}
Public static void main(string args[])
Calculation obj=new calculation();
Obj.sum(10,10);
Obj.sum(20.5,20.5)
}
7. Why method overloading not possible by changing the return type?
• There may occur ambiguity
Class calculation
{
int sum(int a, int b){system.out.println(a+b);}
double sum(int a,int b){system.out.println(a+b);}
Public static void main(string args[])
Calculation obj=new calculation();
Int result=Obj.sum(10,10);// compile error
}
8. Can we overload a main method?
Class simple
{
Public static void main(int a)
{
System.out.println(a);
}
Public static void main(string args[])
{
System.out.println(“method invoked”);
main(10);
}
}
9. Method overloading with type promotion
Class calculation
{
Void sum(int a, long b){system.out.println(a+b);}
Void sum(int a,int b,int c){system.out.println(a+b+c);}
Public static void main(string args[])
Calculation obj=new calculation();
Obj.sum(10,10);// second literal promoted to long
Obj.sum(20,20,20)
}
10. Constructor overloading in java
• Same constructor declared with different parameters in the same class is known as constructor
overloading.
public class Perimeter {
public Perimeter()
{
System.out.println("From default");
}
public Perimeter(int x)
{
System.out.println("Circle perimeter: " + 2*Math.PI*x);
}
public Perimeter(int x, int y)
{
System.out.println("Rectangle perimeter: " +2*(x+y));
}
public static void main(String args[]) {
Perimeter p1 = new Perimeter();
Perimeter p2 = new Perimeter(10);
Perimeter p3 = new Perimeter(10, 20);
} }
11. this() with Constructors
• this() is used to access one constructor from another within the same class.
public class Perimeter {
public Perimeter(){
System.out.println("From default");
}
public Perimeter(int x) {
this();
System.out.println("Circle perimeter: " + 2*Math.PI*x);
}
public Perimeter(int x, int y)
{
this(100);
System.out.println("Rectangle perimeter: " +2*(x+y));
}
public static void main(String args[]) {
Perimeter p3 = new Perimeter(10, 20); } }
12. Rules of using this()
• A few restrictions exist for the usage of this().
• If included, this() statement must be the first one in the constructor. You
cannot write anything before this() in the constructor.
• With the above rule, there cannot be two this() statements in the same
constructor (because both cannot be the first).
• this() must be used with constructors only, that too to call the same class
constructor (but not super class constructor).
14. If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
15. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com