JAVA Constructor and Destructor
01-07-2017    2716 times
java constructor destructor

What is Constructor?[ICSE 2014]

Constructor is method in a class which is initialised object variables, when an object is create.

What are the characteristics of a constructor?

  • A constructor has the same name as the class name.
  • A constructor has no return type, not even void.
  • Access specifies of the constructors are generally public.
  • A constructor is automatically called on object creation.
Tutorial at home

When is a constructor called?[ICSE 2014]

A constructor is called automatically during object creation.

How many Types of constructor possible in java?

There are three types of constructor possible in java

  1. Default Constructor: If programmers do not specify any constructor, then compiler automatically define a constructor called default constructor. If programmers specify any constructor then supply of the default constructor is stop.
  2. Null Constructor: This type of the constructor has not received any parameter. It is generally initialised the objects with default values.
  3. Parameterised Constructor: This type of the constructor has received one or many parameters. It is generally initialised the objects with customised values.
Tutorial at home

What is the difference between a default constructor and a null constructor?

Null constructor is defined by the programmers and initial values are also determined by the programmers. But default constructor is supplied by constructors it doesn’t accept any parameters and initial values are not defined by programmers.

What is the difference between a default constructor and a parameterised constructor?

A default constructor doesn’t accept any parameters. A parameterised constructor accepts parameters.

What does the default constructor provided by the compiler do?

The default constructor provided by the compiler initializes all instances variables to their default values. It initialises integers to 0, floating point numbers to 0.0, boolean values to false and objects to null.

Tutorial at home
Tutorial at home

What is a destructor?

A destructor is a method which is executed when the object is removed from computer’s memory.

Explain the concept of constructor overloading with an example. [ICSE 2011]

A class can have more than one constructor with different singature. This is known as constructor overloading.
Example:

class Car 
{
    int color;
    public Car() { color = -1;}
    public Age(int cl) {color = cl;}

}

freenotes
    Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
    Related Post
  Query About the post