public class constructorexample {
public static void main(String[] args)
{
personcon kumaran=new personcon();
kumaran.display();
personcon jannu=new personcon("Jannu",22);
jannu.display();
}
}
class personcon
{
private String name;
private int age;
//default constructor
personcon()
{
name="kumaran";
age=23;
}
//parameterized constructor
personcon(String n,int a)
{
name=n;
age=a;
}
public void display()
{
System.out.println("Name: "+name+"\t Age: "+age);
}
}
public static void main(String[] args)
{
personcon kumaran=new personcon();
kumaran.display();
personcon jannu=new personcon("Jannu",22);
jannu.display();
}
}
class personcon
{
private String name;
private int age;
//default constructor
personcon()
{
name="kumaran";
age=23;
}
//parameterized constructor
personcon(String n,int a)
{
name=n;
age=a;
}
public void display()
{
System.out.println("Name: "+name+"\t Age: "+age);
}
}
Comments
Post a Comment
Share this to your friends