What is a function overloading ?

627    Asked by AngelaBaker in Salesforce , Asked on Nov 19, 2019
Answered by Vikram Choudhary

Function Overloading is a feature that allows a class to have more than one function having the same name, if their lists of arguments are different.

For example, the argument list of a method multiple(int x, int y) having two parameters is different from the argument list of the method multiple(int x, int y, int z) having three parameters.

There are two ways to do function overloading :-

By changing the number of arguments

Example :-

In this example we are declaring two methods with the same but the number of arguments is different here,

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 TestOverloading1{

public static void main(String[] args){

System.out.println(Addition.add(11,11));

System.out.println(Addition.add(11,11,11));

}}

Output :-

22

33

By changing arguments data types

Example :-

In this example we are declaring two methods with the same but the argument data type is different here,

class Adder{

static int add(int a, int b){return a+b;}

static double add(double, double b){return a+b;}

}

class TestOverloading2{

public static void main(String[] args){

System.out.println(Adder.add(11,11));

System.out.println(Adder.add(12.3,12.6));

}}

Output :-

22

24.9



Your Answer

Answers (2)

Function overloading is a feature in programming that allows a class to have multiple methods with the same name, but different parameters (either in number or type geometry dash). This enables methods to perform different tasks based on the provided arguments. Function overloading is commonly used to create methods that are functionally similar but operate on different data types or different numbers of arguments.

You can define multiple methods with the same name but different numbers of parameters.

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 TestOverloading1 {

    public static void main(String[] args) {

        System.out.println(Addition.add(11, 11)); // Output: 22

        System.out.println(Addition.add(11, 11, 11)); // Output: 33

    }

}

11 Months

How to fix Function Overload problem? doodle baseball

1 Year

Interviews

Parent Categories