Method Overloading in Java

Method overloading in Java is a programming concept when programmer declares two methods of the same name but with different method signature, e.g. change in argument list or change in a type of argument. Method overloading is one of the ways through which Java supports polymorphism.

We may overload the method by changing the argument list of the method. We may change the argument list by —

  • Change number of parameters
  • Change Datatypes of Parameters
  • Change sequence of Datatypes of Parameters

 

Selenium Testing Video Tutorial:

 

Points about Method Overloading:

  • Overloaded methods are bonded using static binding in Java. Which occurs during compile time i.e. when you compile Java program. During the compilation process, compiler bind method calls to the actual method.
  • Overloaded methods are fast because they are bonded during compile time and no check or binding is required during runtime.
  • Method return type doesn’t matter in the case of overloading.

 

Overloading by changing no. of Parameters

We can perform overloading by changing the number of parameters. in below example, we overload the area() method by changing its parameters.

Output :

Area is 120
Area is 600

 

Overloading by changing Datatypes of Parameters

We can perform overloading by changing the datatypes of parameters. In below example, we overload the sum() method by changing datatypes of its parameters.

Output :

sum is 13
sum is 8.4

 

Overloading by changing the sequence  of Datatypes of Parameters

We can perform overloading by changing the sequence of datatypes of parameters. In below example we overload the display() method by changing the sequence of datatypes of its parameters.

Output:

1 I am the f declaration
2 I am the s declaration

0 Comment

Leave a Reply