Login    New User    Forgot Password    Help      

Overloading Constructors

Java Tutorials

In java  method overloading means creating more than a single method with same name with different signatures. In the example three methods are created with same name. Java understands these methods  with there signatures. Java identifies the methods by comparing their signatures like return types, constructor parameters & access modifier used.

Example

class abc
{
int a;
int b;
       
void getdata()
        {
        System.out.println("No argument are passed");
        a=10;
        b=20;
        }
       
void getdata(int i)
        {
        System.out.println("only one argument is passed");
        a=i;
        b=i;
        }
       
void getdata(int i ,int j)
        {
        System.out.println("Both arguments are passed");
        a=i;
        b=j;
        }
       
void showdata()
        {
        System.out.println("Value of a is "+a);
        System.out.println("Value of b is "+b);
        }
}
class ol1
{
public static void main(String args[])
{
abc ob1 =new abc();
abc ob2 =new abc();
abc ob3 =new abc();

ob1.getdata();
ob1.showdata();

ob2.getdata(10);
ob2.showdata();

ob3.getdata(10,20);
ob3.showdata();
}
}

Output

Java Tutorials