Search This Blog

Tuesday, 28 January 2014

constructors in java

/* program illustrating the concept of three constructors in java*/


package constructors;

class Parent
{
    int n;
    String s;
    Parent()  /*Default Constructor Calls*/
    {
        n=2;
        s="sanjay";
        System.out.println("Number is :"+n+" and name is :"+s);
    }
    Parent(int a,String s)/*Parametrised Constructor Calls*/
    {
       n=a;
       this.s=s;
       System.out.println("Number is :"+n+" and name is :"+s);
    }
    Parent(Parent p) /*Copy constructor Calls*/
    {
      n=p.n;
      this.s=p.s;
      System.out.println("Number is :"+n+" and name is :"+s);  
    }
}
class MainClass
{
    public static void main(String[] args)
    {
        Parent a=new Parent();
        Parent b=new Parent(3,"neeraj");
        Parent c=new Parent(b);
    }
}

output


Number is :2 and name is :sanjay
Number is :3 and name is :neeraj
Number is :3 and name is :neeraj

No comments:

Popular Posts