// 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.
*/
1 Rahim 0
2 Ram 25
Press any key to continue . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.