Tutoriallearn.com
Easy Learning
Search Tutoriallearn.com :
class student {
int rollno;
String name;
student(int r, String n) // constructor is defined, it has same name as class name
{
rollno = r;
name = n;
}
student(int r) // constructor is defined, it has same name as class name
{
rollno = r;
name = "Gargi";
}
student (student k) // copy constructor
{
rollno = k.rollno;
name = k.name;
}
display ()
{
System.out.println ("roll no is " + rollno);
System.out.println ("name is "+ name);
}
}
class demostudent {
public static void main (String args[])
{
student s1 = new student(10, "Rajesh"); // two argument’s constructor will be called
student s2 = new student(12) // one argument constructor will be called
student s3 = new student(s2); // copy constructor will be called
s1.diaplay();
s2.display();
}
}
roll no is 10 name is Rakesh roll no is 12 name is Gargi roll no is 12 name is Gargi