Write a program in java to demonstrate overloading?

class OverLoading

{

                String s1;              //Instance member/Attributes

                int a,b;  //Instance member/Attributes

                public OverLoading()

                {

                                a=7;

                                b=9;

                                System.out.println("Default");

                }

                public OverLoading(int x,int y)

                {

                                a=x;

                                b=y;

                                System.out.println("Two Parameterize");

                }

                public OverLoading(String s2,int x)

                {

                                a=x;

                                s1=s2;

                                System.out.println("Two Parameterize Having Different Data Type");

                }

                void add()

                {

                                System.out.println("Addition is "+(a+b));

                }

                void add(int x,int y)

                {

                                a=x;

                                b=y;

                                System.out.println("Addition is "+(a+b));

                }

                public static void main(String arg[])

                {

                                OverLoading x1=new OverLoading();

                                x1.add();

                                OverLoading x2=new OverLoading(7,8);

                                x2.add();

                                OverLoading x3=new OverLoading("Vikrant",10);

                                x3.add();

                                x3.add(11,12);

                }

}
OverLoading.PNG
, , ,

Leave a Reply