Let's learn java programming language with easy steps

Tuesday, 22 August 2017

Interview Questions on Constructor in Java

Java Constructor Interview Questions

Java Constructor Interview Questions

He we are going to cover all the interview questions on constructor in java with their answers. In last article, we learned oops interview questions in java.

(1) What is constructor in java?

Constructor is a special type of method or member function of class.

(2) What is the use of constructor in java?

Constructor is used to initialize the state(data member) of an object i.e putting a value into a data members.

(3) When constructor is invoked?

Constructor is invoked automatically at the time of object creating.

(4) How many types of constructor in java?

There are 2 types of constructor and these are given below
  • Default constructor(no-argument)
  • Parameterized constructor(with argument)
)

(5) When use default and when use parameterized Constructor?

Default Constructor: When we want to provide default of same value to each object then we use default or no-argument constructor.

Parameterized Constructor: When we want to provide different-different values of each or multiple object then we use parameterized constructor.


(6) Does java compiler add any constructor in java program?

Yes, If there is no constructor in our java program then java compiler add default constructor in java program.


(7) What is constructor chaining?

When we call one constructor from another constructor is called constructor chaining. By the help of 'this' keyword we can perform constructor chaining in java.


(8) Can we inherit constructor?

No, We cannot inherit constructor.


(9) Can you make a constructor final?

No, We can't make a constructor final.


(10) Can we declare constructor as private?

Yes, We can declare constructor as private but if you make any constructor as private then you cannot create object of that class from outside of class.

We can also use other access modifiers with constructor like default, public, protected.


(11) Can we use both "this" and "super" in the constructor?

No, We can't use 'this' and 'super' keywords together in the constructor because both 'this' and 'super' should be the first statement in any constructor.

(12) Do we have destructors in Java?

No, There is no destructors in java because java is garbage collected language.


(13) Does constructor return any value?

Yes, It returns current instance.

(14) Can we use return type with constructor?

No, We can't use return type with the constructor.

For example:

class Student
{
void Student()
{
}
}


(15) Should constructor name same as its class name?

Yes, Constructor name must be same as its class name.

For example:

Class Employee
{
Employee(){}//default constructor declaration
}

(16) Can we overload constructor in Java?

Yes, We can overload constructor.

(17) Can we use final, static, abstract keyword with constructor?

No, We cannot make constructor as final, static, and abstract. It is compiler time error.

(18) Can we call sub class constructor from super class constructor?

No, We cannot call.

(19) Difference between constructor and method?


Constructor
  • It is used to initialize the state of an object.
  • Constructor name must be same as the class name.
  • Constructor should not have any return type.

Method
  • It is used to expose behavior of an object i.e task or action.
  • Method name may or may not be same as class name.
  • Method must have return type.

Read More :

Java Polymorphism Interview Questions.
Java OOPS Interview Questions.
Servlet Interview Questions.
Struts Interview Questions.

Here we learned most important and common java constructor interview Questions and Answers .
Share:

3 comments:

  1. Wonderful article! We are linking to this great post on our site.Keep up the good writing. Visit: Please Read More: Download Ebook: Ultimate Guide To Job Interview Questions Answers:

    ReplyDelete
  2. Thank you for sharing this java constructor interview questions.

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate