Google Search

Sunday, October 30, 2011

Write a Java program using method overloading.


  1. Write a program using method overloading.

class Overload
{
      void ovlDemo()
      {
                  System.out.println("No Parameters");
      }
      void ovlDemo(int a)
      {
                  System.out.println("One Parameter:"+a);
      }
      int ovlDemo(int a,int b)
      {
                  System.out.println("Two Parameters:"+a+" "+b);
                  return a+b;
      }
      double ovlDemo(double a,double b)
      {
                  System.out.println("Two Double Parameters:"+a+" "+b);
                  return a+b;
      }
}

class OverloadDemo
{
      public static void main(String args[])
      {
                  Overload ob=new Overload();
                  int resI;
                  double resD;
                  ob.ovlDemo();
                  System.out.println();
                  ob.ovlDemo(2);
                  System.out.println();
                  resI=ob.ovlDemo(4,6);
                  System.out.println("Result of ob.ovlDemo(4,6): "+resI);
                  System.out.println();
                  resD=ob.ovlDemo(1.1,2.32);
                  System.out.println("Result of ob.ovlDemo(1.1,2.32): "+resD);
      }
}

1 comments:

Post a Comment