Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,913 questions

51,845 answers

573 users

How to implement method overloading based on the number of arguments in Java

1 Answer

0 votes
public class MyClass {
    static int sum(int a, int b) {
        int sm = 0;

        sm = a + b;

        return sm;
    }

    static int sum(int a, int b, int c) {
        int sm = 0;

        sm = a + b + c;
        
        return sm;
    }

    static int sum(int a, int b, int c, int d) {
        int sm = 0;

        sm = a + b + c + d;
        
        return sm;
    }
    
    public static void main(String args[]) {
        int result = sum(5, 7);
        System.out.println("Sum = " + result);

        result = sum(34, 5, 87);
        System.out.println("Sum = " + result);

        result = sum(1, 54, 73, 958);
        System.out.println("Sum = " + result);
    }
}




/*
run:
  
Sum = 12
Sum = 126
Sum = 1086
   
*/

 



answered Jan 27, 2024 by avibootz
...