Skip to main content

Practical-17

Write a java program which shows the use of Methods Overloading.


Introduction

The concept of method overloading in Java is used to allow a class with multiple methods of the same name, but different parameters. This method can be used to increase the readability of the code as well as giving more flexibility to developers. By allowing the use of different combinations of parameters and return types, the same program can be used for different purposes with different inputs. It is an important concept for object-oriented programming, and can be used to greatly improve the code's readability, modularity, and flexibility.

Code

public class OverloadingExample{
static int add(int a, int b){return a+b;}
static int add(int a, int b, int c){return a+b+c;}

public static void main(String args[]){
System.out.println(OverloadingExample.add(11,11));
System.out.println(OverloadingExample.add(11,11,11));
}}

Output

22
33
Explanation

The example code above is a demonstration of method overloading in Java. Overloaded methods are distinguished from one another by their parameters. The same method name is used, but with different parameter combinations. The first method has two int type parameters, and the second has three int type parameters. The output of the code demonstrate the correct functionality of the overloaded methods. :::