Pages

Sunday, 25 September 2016

Default Constructor and Parameter Constructor Example

public class cube
{
 public int no;

 cube()
   {
     no=10;
   }

 cube(int n)
   {
     no=n;
   }

 public int ans()
   {
     return(no*no*no);
   }
}

public class test
 {
    public static void main(string[] args)
  {

   cube t1=new cube();
   System.out.println("the cube value of a no="+t1.no+" is "+t1.ans());

   cube t2=new cube(3);
   System.out.println("the cube value of a no="+t2.no+" is "+t2.ans());
  }
}

No comments:

Post a Comment