Sunday, 25 June 2017

Method overloading in java ~ foundjava

As Java is OOP language, it supports method overloading

class methodOverloading
{
// overloading with change in number of parameters

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

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

// overloading with change in types of parameters

        public String add(String s1,String s2)
        {
        return s1+" "+s2;
        }

        public static void main(String args[])
        {
        methodOverloading m=new methodOverloading();
        System.out.println("The sum of two numbers is "+m.add(10,20));
        System.out.println("The sum of three numbers is "+m.add(10,20,30));
        System.out.println("The Full name is "+m.add("Gowtham","Gutha"));
        }
}
add(int a,int b) : Takes two integer values and returns their sum
add(int a,int b,int c) : Takes three integer values and returns their sum
add(String s1,String s2) : Takes two strings and join them with a space

No comments:

Post a Comment