Constructor in java with interview questions and answers.

As we discussed in the previous tutorials what is oops concept like. classes and object, inheritance, polymorphism, encapsulation, abstraction in java. In this tutorial, we will focus on constructor in java but before the start, If you are new to java please have a look at class and object tutorial.

What is the constructor in Java?

A constructor is used to initialize the value to the object. A constructor contains variables and other statements which are executed at the time of object creation.

How to define a constructor in Java and how to initialize the value to an object.

There is some rule you have to follow to define a constructor in java.

  • Constructor name should be same as class name.
  • There is no return type for a constructor.

Syntax:

class_name(){}
class Main {
  Main(int a) {
    System.out.println("Hello Codebun " + a);
  }

  public static void main(String args[]) {
    new Main(10);
  }
}

Type of constructor in Java.

Default constructor in Java:

A constructor without any parameter is called default constructor in no argument constructor.

note: If you do not define any constructor in java. Compiler automatic will add a default constructor for you.

class Main {
  Main() {
    System.out.println("Hello Codebun ");
  }

  public static void main(String args[]) {
    new Main();
  }
}

 Parameterized constructor in Java:

Constructor with the parameter is called the parameterized constructor in Java. for example, we have a constructor Main(int a, int b){} at the time of object creation we will pass an integer value.

class Main {
  Main(int a, int b) {

    System.out.println("Hello Codebun " + a * b);
  }

  public static void main(String args[]) {
    new Main(10, 20);
  }
}

Constructor Overloading in Java.

We can overload constructor like method overloading. remember one rule no. of parameters will defer.

class Main {
  Main(int a, int b) {
    System.out.println(a + b);
  }

  Main(int a, int b, int c) {
    System.out.println(a + b + c);
  }

  public static void main(String args[]) {
    new Main(10, 20);
    new Main(10, 20, 30);
  }

Some important point about constructor in Java.

  • If you do not define any constructor in java. Compiler automatic will add a default constructor. So every java class has a constructor.
  • Constructor name should we same as a class name.
  • There is no return type in constructor. but constructor returns a current class instance.
  • The copy constructor is not supported in java.

Java Constructor interview questions and answers with the example.

What is constructor chaining?

Calling one constructor from another constructor is called constructor chaining. by using this or super keyword. we can call a constructor from another constructor for example like the code below or click here.

class Main {
  int a, b;

  Main() {
    System.out.println("Default Constructor from demo class");
  }

  Main(int a, int b) {
    this();
    this.a = a;
    this.b = b;

  }

  public void display() {
    System.out.println(a + " " + b);
  }

  public static void main(String[] args) {
    Main obj = new Main(10, 20);
    obj.display();
  }

}

Can we use access modifiers with the constructor?

Yes, we can use access modifiers with the constructor. If we define any constructor as a private then we can use it only within the class.

Can we define a method with the same name of the class?

Yes, we can define but it will give runtime error.

Why is constructor name same as the class name?

The constructor returns a current class object so when we create an object with new keyword and class name. so constructor name would be same as a class name.

If the class has an explicit constructor, will it have default constructor?

No, the only compiler defines default constructor if there is no explicit constructor in Java.

 

Add a Comment

Your email address will not be published. Required fields are marked *

three × five =