Java Program using constructor overloading


In this program, You will learn how to implement constructor overloading in java.


Main() { 
    //statement
}

Main(int y) { 
    //statement
}

Example: How to implement constructor overloading in java.

class Main {
    int x;
    Main() {
        x = 10;
    }

    Main(int y) {
        x = y;
    }

    void display() {
        System.out.println("The x value is :" + x);
    }

    public static void main(String args[]) {
        Main t1 = new Main();
        t1.display();

        Main t2 = new Main(20);
        t2.display();
    }
}

Output:

The x value is :10
The x value is :20