Tuesday, December 10, 2013

Constructor in JAVA : Day 5



 a) What happened when a constructor initialized in an Object?


b) Write a program to find the volume of a Box. Use Constructor to initialize the dimensions of a Box. Compute and Return the result using Method.

3 comments:

  1. ****Write a program to find the volume of a Box. Use Constructor to initialize the dimensions of a Box. Compute and Return the result using Method.

    source code:

    package Box;

    class Box
    {
    double width;
    double height;
    double depth;

    // this is the constructor for box

    Box(double w,double h,double d)
    {
    width=w;
    height=h;
    depth=d;
    }

    //computr the return volume

    double volume()
    {
    return width*height*depth;
    }
    }
    class BoxDemo
    {
    public static void main(String args[])
    {
    //declare, allocate and initialize box objects

    Box mybox1=new Box(5,10,15);
    Box mybox2=new Box(3,6,9);
    double vol;

    //get volume of the first box
    vol = mybox1.volume();
    System.out.println("Volume is " + vol);

    //get volume of the 2nd box
    vol = mybox2.volume();
    System.out.println("Volume is " + vol);
    }
    }

    ReplyDelete
  2. *** What happened when a constructor initialized in an Object ?

    => It can be tedious to initialize all of the variables in a class each time an instance is created. The requirement for initialization is so common. Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor.
    Constructor has the same name as the class in which it resides and is syntactically similar to a method. The constructor is automatically immediately after the object is created, before the new operator completes.Constructor has
    no return type, not even void. This is because the implicit return type of a class constructor is the class type itself.
    Constructor`s job is to initialize the internal state of an object so that the code creating an instance will have a fully initialized usable object immediately.
    If a constructor for a class does not defined explicitly, then java creates a default constructor for the class. The default constructor automatically initializes all instance variables to zero. The default constructor is often sufficient for simple classes, but it usually won`t do for sophisticated ones.

    ReplyDelete
  3. ********What happened when a constructor initialized in an Object?********
    Solution:
    Constructors Initializing as an class object: Objects contain their own copy of Instance Variables. It is very difficult to initialize each and every instance variable of each and every object of class. Java allows objects to initialize themselves when they are created. Automatic initialization is performed through the use of a constructor. A constructor initializes an object as soon as object gets created. Constructor gets called automatically after creation of object and before completion of new operator.

    ReplyDelete