Wednesday, December 14, 2016

Write a JAVA program demonstrating the difference between method overloading and constructor overloading.

 Write a JAVA program demonstrating the difference between method overloading and constructor overloading.

class Student
{
    int id;
    String name;
    int age;
    Student(int i,String n)
    {
       id = i;
       name = n;
    }
    Student(int i,String n,int a)
    {
       id = i;
       name = n;
       age=a;
    }

    void display()
    {
      System.out.println(id+" "+name+" "+age);
    }
    void sum(int a,int b)
    {
     System.out.println(a+b);
    }
    void sum(double a,double b)
    {
     System.out.println(a+b);
    }
    public static void main(String args[])
    {
    Student s1 = new Student(111,"hasini");
    Student s2 = new Student(222,"shirley",1);
 
    s1.display();
    s2.display();
    s1.sum(10.5,10.5);
    s1.sum(20,20);
   }
}


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home