top of page

METHOD OVERLOADING IN JAVA


 If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.


Advantage of Method Overloading: If we have to perform only one operation, having same name of the methods increases the readability of the program.


There are Two different ways to overload the method:

1.      By Changing number of arguments

2.      By changing the data type

 

Example of Method Overloading: By changing number of arguments

In this example, we have created two methods. First mul() performs multiplication of two numbers while second mul() performs multiplication of three numbers.


class Mul

{  

static int mul(int a, int b)

{     return a*b;   

}  

static int mul(int a, int b, int c)

{     return a*b*c;          

 }  


class Example

{  

public static void main(String[] args)

{     System.out.println(Mul.mul(7,8));  

System.out.println(Mul.mul (7,8,2)); 

 }

}  

 

OUTPUT:

56

112

 

Example of Method Overloading: By changing the data type

In this example, we have created two methods with same name using different data types. The first mul() method receives two integer arguments and second mul() method receives two double arguments.


class Mul

static int mul(int a, int b)

{           return a*b;

static double mul(double a, double b)

{           return a*b;


class Example1

public static void main(String[] args)

{           System.out.println(Mul.mul(7,8)); 

System.out.println(Mul.mul(7.6,8.3)); 

}


OUTPUT:

56

63.08

 

 

 

 
 
 

Recent Posts

See All

Comments


MiIT Logo

Company

Contact Us

+1905-487-4880 

5160 Explorer Dr #34, Mississauga,ON L4W 4T7

+1929-743-3199

4466 Buttonwood Ln Lilburn, GA 30047

262 Chapman Rd, STE 240 Newark DE 19702

+44 20 4525 1214

71-75 Shelton Street, Covent Garden, London, United Kingdom WC2H 9JQ

Stay up to date on the latest from MiIT

  • Instagram
  • Facebook
  • http://linkedin.com/company/miittechnologies/about/
  • Whatsapp

© All Content MiIT Technologies Inc.2019 - 2025. All rights reserved.

bottom of page