Constructor Overloading in Java With Example

By | December 17, 2010
Constructor Overloading in Java With Example.You can find Constructor Overloading in Java Source code (code snippets) in this page.

class Student3{

private int rollNumber;

private String name;

public Student3()               {

System.out.println(“Inside Default Constructor”);

rollNumber=0;

name=””;

}

public Student3(int rollNumber,String name)             {

System.out.println(“Inside Overloaded Constructor”);

this.rollNumber=rollNumber;

this.name=name;

}

public void displayStudent()                   {

System.out.println(“rollno= “+rollNumber+”name “+name);

}

}

class ConstructorOverLoad{

public static void main(String arg[])

{

Student3 s1=new Student3(101,”jack”);

s1.displayStudent();

Student s2=new Student();

s2.displayStudent();

}

}

Result:

Inside Overloaded Constructor

rollno= 101name jack

Inside Default Constructor

rollno= 0 name

Leave a Reply

Your email address will not be published. Required fields are marked *