Constructor Overloading in Java

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

1178

Program:

// Constructor Overloading
	
 class StudentClass{
    int rollno;
    String name;
    int age;

    StudentClass(int i,String n){
    rollno = i;
    name = n;
    }

    StudentClass(int i,String n,int a){
    rollno = i;
    name = n;
    age=a;
    }

    void display(){
		System.out.println(rollno+" "+name+" "+age);
		}

    public static void main(String args[]){
    StudentClass s1 = new StudentClass(1,"Rahim");
    StudentClass s2 = new StudentClass(2,"Ram",25);
    s1.display();
    s2.display();
   }
}

/*

Constructor overloading is a technique in Java in which a class can have any
number of constructors that differ in parameter lists.The compiler
differentiates these constructors by taking into account the number of
parameters in the list and their type.

*/

Output:

1 Rahim 0
2 Ram 25
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.