Constructors
Constructors
Question 1
A member method having the same name as that of the class is called ............... .
1. an alias
2. a friendly method
3. a constructor
4. a protected method
Answer
a constructor
Reason — A member method having the same name as that of the class is called constructor.
Question 2
1. a void
2. more than one
3. String[] args
4. no
Answer
no
Question 3
1. created
2. destroyed
3. assigned a value
4. abstracted
Answer
created
Question 4
A default constructor has ............... .
1. no parameters
2. one parameter
3. two parameters
4. multiple parameters
Answer
no parameters
Question 5
Answer
Question 6
1. All constructors must have the same name as that of the class.
2. All constructors must have the same number of arguments.
3. All constructors must have arguments of type String[] args.
4. All constructors must have no arguments.
Answer
All constructors must have the same name as that of the class.
Reason — In constructor overloading, all constructors must have the same name as that of
the class.
Assignment Questions
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
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
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:
Question 7
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;
Question 8
Answer
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.
Question 9
Create a class named Pizza that stores details about a pizza. It should contain the following:
Instance Variables:
String pizzaSize — to store the size of the pizza (small, medium, or large)
int cheese — the number of cheese toppings
int pepperoni — the number of pepperoni toppings
int mushroom — the number of mushroom toppings
Member Methods:
Constructor — to initialise all the instance variables
CalculateCost() — A public method that returns a double value, that is, the cost of the pizza.
Pizza cost is calculated as follows:
Small: Rs.500 + Rs.25 per topping
Medium: Rs.650 + Rs.25 per topping
Large: Rs.800 + Rs.25 per topping
PizzaDescription() — A public method that returns a String containing the pizza size,
quantity of each topping, and the pizza cost as calculated by CalculateCost().
if (pizzaSize == "small")
totalCost = 500 + topCost;
else if(pizzaSize == "medium")
totalCost = 650 + topCost;
else
totalCost = 800 + topCost;
return totalCost;
}
return desc;
}
Output