Constructors Java: Learn through Questions and Answers


One of the simplest methodologies of learning a subject is through Questions/Answers. I believe in this. 17 questions with answers on Constructors Java are given.

1. What is constructor?
2. Define constructor in Java?
3. What is class constructor in Java?
4. What is the return of constructor?

One of the Java constructs (with which a program can be constructed) is Constructor. The other constructs are variables, methods and static blocks etc.

a) A constructor looks like a method but without a return type.
b) Moreover, the name of the constructor and class name should be the same.

The compiler knows the constructor by its name (same class name) and return type (no return type including void).

5. How to create a constructor in Java?

It is very simple. Infact, to create an object of a class, we call the constructor without our knowledge. That is, we ignore that we are calling a constructor (as our concentration will be on creating object).

See the simple code on Constructors Java here.
public class Student
{
  public Student()                     // defining a constructor
  {
    System.out.println("Hello 1");
  }
  public static void main(String args[])
  {
    Student std1 = new Student();      // creating object to call the constructor
    Student std2 = new Student();
    Student std3 = new Student();
  }
}

Constructors Java

public Student()

The above statement defines a constructor. Observe, the class name and constructor name are same. Just to know that constructor is called, a println() statement is given.

Student std1 = new Student();
Student std2 = new Student();
Student std3 = new Student();

In the main() method, three objects std1, std2 and std3 are created. Each object calls the constructor and prints Hello 1. That is, three times the constructor is called and Hello 1 is printed three times.

Student std1 = new Student();

In the above statement, the last word Student() denotes a constructor. It infers, to create an object, compulsorily constructor is to be called.

Note: Do not try to call a constructor like a method. std1.Student() raises compilation error.

6. What is default constructor in Java?
7. What is no argument constructor Java?

A constructor without any arguments is known as no-args constructor. This is also known as default constructor because it is created and supplied into the program by the JVM if the Programmer does not create of his own.

In the previous program, the following constructor is known default constructor.

  public Student()                                       
  {
    System.out.println("Hello 1");
  }

Observe, the above constructor does not carry any arguments. This is known as default constructor.

8. What is Parameterized constructor in Java?

Just like methods, a constructor also can take parameters and is known as Parameterized constructor. Note that, the default constructor does not have any parameters.

Example on Constructors Java
public class Student
{
  public Student(String name, int marks)          // parameterized constructor
  {
    System.out.println("Student name is " + name + " and marks are " + marks);
  }
  public static void main(String args[])
  {
    Student std1 = new Student("Mr.Rao", 78);
    Student std2 = new Student("Mr.Reddy", 69);
    Student std3 = new Student("Mr.Raju", 56);
  }
}

Constructors Java

public Student(String name, int marks)

As Student(String name, int marks) constructor is carrying parameters, it is known as parameterized constructor.

9. How many types of constructors exist in Java?
10. What are different types of constructors in Java with examples?

There are two types of constructors in Java.

a) Default constructor
b) Parameterized constructor

11. Is it possible to have a Java class without constructor?

By rule it is possible to have, but it does not have any practical implication as we create atleast one object in the code. That is, we do not dream of a Java program without creating an object.

See the following code without constructor and with just main() method.

Example code on Constructors Java
public class Student
{
  public static void main(String args[])
  {
    System.out.println("Hello World");
  }
}

ima2

12. How many constructors a class can have?

A class can have one default constructor and any number of parameterized constructors.

13. Can we overload constructors in Java?

Yes, definitely. Like methods, constructors also can be overloaded.

Writing the same constructor multiple times in the same class but with different parameters is known as constructor overloading.

See the following code.

public class Student
{
  public Student()                        // I ,  default constructor
  {
    System.out.println("Hello 1");
  }
  public Student(String name)             // II,  parameterized constructor with single parameter
  {
    System.out.println("Student name is " + name);
  }

  public Student(String name, int marks)    // III,  parameterized constructor with two parameters
  {
    System.out.println("Student name is " + name + " and marks are " + marks);
  }

  public static void main(String args[])
  {
    Student std1 = new Student();                 // calls I
    Student std2 = new Student("Mr.Reddy");       // calls II
    Student std3 = new Student("Mr.Raju", 56);    // calls III
  }
}

Constructors Java

The Student() constructor is overloaded three times.

14. What is the importance of Constructors in Java?
15. What are the uses of Constructors in Java?

Basically, there are two advantages.

a) As everyone says, it is called implicitly.

b) A constructor gives properties to an object at the time of creation itself. That is, a Programmer can assign the properties (values) to an object while it is being created. Let us see the meaning through Java API classes.

For example take the object of TextField.

  TextField tf1 = new TextField();
  tf1.setColumns(30);
  tf1.setText("hello");

In the above code, an object tf1 of TextField is created by calling the default constructor. Later two properties are given of columns (no. of characters to display at a time) and the text to display. Here TextField object is created and later by calling two methods, the properties are assigned. Let us do the same job by calling an overloaded constructor.

  TextField tf1 = new TextField("Hello", 30);

In the above statement also tf1 object is created and also assigned two properties. The difference with the previous is less code of escaping two statements. The properties Hello and 30 are given to tf1 at the time of creation itself. This is the main advantage of constructors. In Spring framework, it is called constructor dependency injection (injecting the properties to an object through constructor).

16. What is this() and super() in constructors Java?

We know a constructor can be overloaded. To access one constructor from another within the same class, this() is used. To access super class constructor from a subclass constructor, we use super().

17. Can a constructor be overridden?

In Java, a constructor (or a static method) cannot be overridden.

18. What are properties of Constructors Java?

a) Do not apply final, native, static, strictfp, synchronized or abstract keywords. A constructor cannot take these attributes.
b) Constructor cannot exist in an interface.
c) No return type (including void) to a constructor.
d) A constructor can be declared private.
e) Constructor can exist in an abstract class.
f) A Constructor Java can be overloaded.

Other Constructor related Topics

Leave a Comment

Your email address will not be published.