Home Java Programming Language / Programs / Constructor Overloading in Java
🚀 Programming Example

Constructor Overloading in Java

👁 1,291 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.
No previous program
No next program

💻 Program Code

// 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.

*/
                        

🖥 Program Output

1 Rahim 0
2 Ram 25
Press any key to continue . . .
                            
No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.