0% found this document useful (0 votes)
4 views21 pages

Java Method Overloading Explained

Uploaded by

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

Java Method Overloading Explained

Uploaded by

pratikgarg9480
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Method overloading

Introduction
• If a class has multiple methods having same name but different in
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.
• Important (Method Signature) – must be different
Ways to perform method overloading

• There are two ways to overload the method in java


• By changing number of arguments
• By changing the data type
Changing no. of arguments
class Addition{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading{
public static void main(String[] args){
[Link]([Link](13,13));
[Link]([Link](10,11,10));
}}
Output:
26
31
Changing data type of arguments

class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class TestOverloading{
public static void main(String[] args){
[Link]([Link](10,15));
[Link]([Link](12.3,12.6));
}}
Output:
25
24.9
class Adder{
static int add(int a,int b){return a+b;}
static double add(int a,int b){return a+b;}
}
class TestOverloading{
public static void main(String[] args){
[Link]([Link](11,11)); //ambiguity
}}
• Compile Time Error: method add(int,int) is already defined in class
Adder
• [Link]([Link](11,11)); //Here, how can java
determine which sum() method should be called?
Type Promotion

• One type is promoted to another implicitly if no matching datatype is found.


As displayed in the diagram, byte can be
promoted to short, int, long, float or double.
The short datatype can be promoted to int,
long, float or double.
The char datatype can be promoted to int,
long, float or double and so on.
class OverloadingCalculation
{
void sum(int a,long b){[Link](a+b);}
void sum(int a,int b,int c){[Link](a+b+c);}

public static void main(String args[])


{
OverloadingCalculation obj=new OverloadingCalculation();
[Link](20,20);//now second int literal will be promoted to long
[Link](20,20,20);
}
}
class OverloadingCalculation
{
void sum(int a,int b) {[Link]("int arg method invoked");}
void sum(long a,long b) {[Link]("long arg method invoked");}

public static void main(String args[]){


OverloadingCalculation obj=new OverloadingCalculation();
[Link](20,20);//now int arg sum() method gets invoked
}
}

If there are matching type arguments in the method, type promotion is not performed.
class OverloadingCalculation{
void sum(int a, long b) {[Link]("a method invoked");}
void sum(long a, int b) {[Link]("b method invoked");}

public static void main(String args[]){


OverloadingCalculation obj=new OverloadingCalculation();
[Link](20,20);//now ambiguity
}
}
Output: Compile Time Error
Constructor overloading
public class Student {
// instance variables of the class The purpose of constructor overloading:
int id;
Sometimes, we need to use multiple constructors to initialize
String name;
the different values of the class.
Student() {
[Link]("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
//object creation
Student s = new Student();
[Link]("\nDefault Constructor values: \n");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
[Link]("\nParameterized Constructor values: \n");
Student student = new Student(10, "David");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
this () in constructor overloading

class Student {
int age;
// Constructor-1
Student() {
this (20);
}
// Constructor-2
Student(int age) {
[Link] = age;
}
public static void main(String[] args) {
Student s1 = new Student ();
Student s2 = new Student (12);
}
public class Student {
//instance variables of the class
int id,passoutYear;
String name,contactNo,collegeName;
Student(String contactNo, String collegeName, int passoutYear){
[Link] = contactNo;
[Link] = collegeName;
[Link] = passoutYear;
}
Student(int id, String name){
this("9999111255", “LNMIIT", 2011);
[Link] = id;
[Link] = name;
}
public static void main(String[] args) {
//object creation
Student s = new Student(101, "John");
[Link]("Printing Student Information: \n");
[Link]("Name: "+[Link]+"\nId: "+[Link]+"\nContact No.: "+[Link]+"\nCollege Name: "+[Link]+"\
nPassing Year: "+[Link]);
}
• In Java, this is a reference variable that refers to the current object.

• The this keyword in Java is a special reference variable that points to the
current object within a class.
• It is used in methods and constructors to refer to the instance of the class on
which the method or constructor is currently operating.
this: to refer current class instance variable
class Student{ class Student{
int rollno; int rollno;
String name; String name;
float fee; float fee;
Student(int rollno,String name,float fee){ Student(int rollno,String name,float fee){
rollno=rollno; [Link]=rollno;
name=name; [Link]=name;
fee=fee; [Link]=fee;
} }
void display() void display()
{[Link](rollno+" "+name+" "+fee);} {[Link](rollno+" "+name+" "+fee);}
} }
class TestThis1{ class TestThis1{
public static void main(String args[]){ public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f); Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f); Student s2=new Student(112,"sumit",6000f);
[Link](); [Link]();
[Link](); [Link]();
Program where this keyword is not required
class Student{
int rollno;
String name;
float fee;
Student(int r,String n,float f){
rollno=r;
name=n;
fee=f;
}
void display(){[Link](rollno+" "+name+" "+fee);}
}
class TestThis3{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
[Link]();
[Link]();
}}
this: to invoke current class method
class A{
void m(){[Link]("hello m");}
void n(){
[Link]("hello n");
//m();//same as this.m()
this.m();
}
}
class TestThis4{
public static void main(String args[]){
A a=new A();
a.n();
}}
3) this() : to invoke current class constructor
class A{
A(){[Link]("hello a");}
A(int x){
this();
[Link](x);
}
}
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}}
class Student{
int rollno;
String name,course;
float fee;
Student(int rollno,String name,String course){
[Link]=rollno;
[Link]=name;
[Link]=course;
}
Student(int rollno,String name,String course,float fee){
this(rollno,name,course);//reusing constructor
[Link]=fee;
}
void display(){[Link](rollno+" "+name+" "+course+" "+fee);}
}
class TestThis7{
public static void main(String args[]){
Student s1=new Student(111,"ankit","java");
Student s2=new Student(112,"sumit","java",6000f);
[Link]();
[Link]();
this: to pass as an argument in the method

• The this keyword can also be passed as an argument in the method. It is mainly used in the event handling.

[Link] S2{
2. void m(S2 obj){
3. [Link]("method is invoked");
4. }
5. void p(){
6. m(this);
7. }
8. public static void main(String args[]){
9. S2 s1 = new S2();
10. s1.p();
11. }
12.}
this: to pass as argument in the constructor call

• We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple
classes.
[Link] B{
2. A4 obj;
3. B(A4 obj){
4. [Link]=obj;
5. }
6. void display(){
7. [Link]([Link]);//
using data member of A4 class
8. }
9.}
[Link] A4{
11. int data=10;
12. A4(){
13. B b=new B(this);
14. [Link]();
15. }
16. public static void main(String args[]){
17. A4 a=new A4();
18. }
19.}

You might also like