Pages

Program for Polymorphism



class PolyDemo
{
  void fun1()
  {
   System.out.println(“Inside fun1 without parameters “);
 }
 void fun1(int x)
  {
   System.out.println(“Inside fun1 with one int parameters “);
   System.out.println(x);
 }
void fun1(double x)
  {
   System.out.println(“Inside fun1 with one double parameters “);
   System.out.println(x);
 }
void fun1(char x)
  {
   System.out.println(“Inside fun1 with one char parameters “);
   System.out.println(x);
 }
void fun1(int x,int y)
  {
   System.out.println(“Inside fun1 with two int parameters “);
   System.out.println(x);
   System.out.println(y);
 }
void fun1(double x,double y)
  {
   System.out.println(“Inside fun1 with two double parameters “);
   System.out.println(x);
   System.out.println(y);
 }
public static void main(String args[])
{
  PolyDemo p = new PolyDemo();
  p.fun1();
  p.fun1(12.5);
  p.fun1(250);
  p.fun1(‘q’);
  p.fun1(100,200);
  p.fun1(12.5,22.5);
 } // end of main()
}// end of class
Note : -> println() is an example for overloaded method since in the java.io.PrintStream class multiple println() methods are defined each to accept values of corresponding Primitive datatypes.