IT Developer

Constructors in Java

Chapter 11

Constructors in Java

Class 11 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Assignment Questions and Programs


    Question 1

    What is a constructor? Why do you need a constructor?

    Answer

    A constructor is a member method of a class that is used to initialise the instance variables. It has the same name as that of its defining class and does not have a return type.

    A constructor is needed as it instructs the computer to perform the start-up tasks of a new object. These can be like assigning default values to instance variables, aligning all the buttons on a screen when a new window object is created, or moving the cursor to a specific data entry field for the user input.


    Question 2

    If the constructor is automatically generated by the compiler, why do you need to define your own constructor?

    Answer

    A default constructor does not have any statement inside it. It initializes default values to the instance variables.

    If we define our own constructor in a class, we can initialise the instance variables according to our requirements. A parameterised constructor allows the programmer to initialise objects with different values. This is achieved by passing the required values as arguments to the constructor method.


    Question 3

    Explain the statement, "you cannot invoke constructors as normal method calls".

    Answer

    A constructor cannot be explicitly invoked like a method. It is automatically invoked via the new operator at the time of object creation. On the other hand, a method can be called directly by an object that has already been created. Normal method calls are specified by the programmer.

    The below program highlights the difference in the way the constructor is invoked versus a normal method call:

    class Test  {

        int a, b;

        public Test(int x, int y)   {

            a = x;

            b = y;

        }

        void add()  {

            System.out.println(a + b);

        }

    }

     

     class invokeConst   {

        public static void main(String args[])  {

            Test obj1 = new Test(10, 20);   // constructor invoked

            obj1.add();    // calling add() method

        }

    }


    Question 4

    Describe the importance of parameterised constructors.

    Answer

    Parameterized constructors are important because they provide a way to initialize objects with different values. For example, if we have a class representing a bank account, we could use a parameterized constructor to initialize a bank account with a specific balance, rather than always starting with a balance of zero. This makes the class more flexible and reusable, as it can be used to create objects with a variety of different initial states.


    Question 5

    If a class is named DemoClass, what names are allowed as constructor names in the class DemoClass?

    Answer

    The constructor can only have the name of the class in which it is defined. Thus, all the constructors of class DemoClass will be named DemoClass.


    Question 6

    Explain the concept of constructor overloading with an example.

    Answer

    The process of creating more than one constructor with the same name but with different parameter declarations is called constructor overloading.

    The below example shows constructor overloading being used to create a class having three constructors:

    public class Rectangle

     {

        int length;

        int width;

     

         //Constructor without any parameters

        public Rectangle()   {

            System.out.println("Invoking constructor with no parameters");

            length = 20;

            width = 15;

        }

         // Constructor with one parameter

        public Rectangle(int len)   {

            System.out.println("Invoking constructor with one parameter");

            length = len;

            width = 20;

        }

         // Constructor with two parameters

        public Rectangle(int  len, int  wd)    {

            System.out.println("Invoking constructor with two parameters");

            length = len;

            width = wd;

        }

     

         public void Area()    {

            int area = length * width;

            System.out.println("Length = " + length);

            System.out.println("Width = " + width);

            System.out.println("Area = " + area);

        }

     

         public static void main(String args[])  {

            Rectangle rect1 = new Rectangle();

            rect1.Area();

             Rectangle rect2 = new Rectangle(30);

            rect2.Area();

             Rectangle rect3 = new Rectangle(30, 10);

            rect3.Area();

        }

    }


    Question 7

    What is the use of the keyword this?

    Answer

    Within a constructor or a method, this is a reference to the current object — the object whose constructor or method is being invoked. The keyword this stores the address of the currently-calling object.

    For example, the below program makes use of the this keyword to resolve the conflict between method parameters and instance variables.

    class  Area

    {

        int  length,  breadth;

         public Area(int length, int breadth) {

            this.length = length;

            this.breadth =  breadth;

        }

            public void Display() {

            int area = length * breadth;

            System.out.println("Area is " + area);

        }

         public static void main(String args[J) {

        Area area = new Area(2, 3);

        area.Display();

        }

    }


    Question 8

    What is a no-argument constructor? Does every class have a no-argument constructor?

    Answer

    A no-argument constructor is a constructor that accepts no arguments and has no statements in its body. It is also called the default constructor.

    If the programmer has not explicitly defined a constructor for the class then Java compiler automatically adds a no-argument constructor/default constructor. However, if the constructor is defined explicitly then no-argument constructor is not added.