New Delhi, India
minigranth@gmail.com

Constructors In Java

Created with Sketch.

Core Java Tutorial

[fblike]

Constructors In Java : Introduction

  • Constructors in java are very special and have unique functionalities. We can initialize objects in Java as soon as they are created. This object initialization is performed by a special method called
  • A constructor is a special method that initializes an object when they are created. Constructors in java are automatically invoked when objects are created.

 

Constructors In Java : Defining Rules

  • Before defining constructors in java, we must know some rules. These are :
    • Constructor should have the same name as that of class name.
    • Constructor do not have any return type not even void.
  • If you do not explicitly define a constructor for a class, then Java creates a default constructor for the class.

 

Constructors In Java : Types

  • There are two Types of Constructors in Java that are used according to the need and logic. These are
    • Default Constructor
    • Parameterized Constructor
  • Each of these constructors is explained below along with its syntax and a sample program.

 

1) Default Constructor

  • These constructors do not take any parameters.

 

Syntax

classname() {

//body of default constructor

}

 

  • An example could be :

Cars() {  

            //body of default constructor

            }

  • The default constructor if not specified is automatically created by Java.
  • Let’s modify the previous Area example using default constructor.

 

Example

 

This image describes a sample program for default constructors in java.

Default Constructors : Example

 

Output

 

This image describes the output of sample program for default constructors in java.

Default Constructors : Output

 

2) Parameterized Constructor

  • Those constructors which takes parameters for initializing object are called parameterized constructors.

 

Syntax

classname(parameter list…) {     }

 

  • An example could be : 

Cars(int speed, int price)

{

            //body of parameterized constructor

}

 

Example

  • Let’s take the same example and add parameterized constructor in it.
This image describes the sample program for parameterized constructors in java.

Parameterized Constructors : Example

 

Output

This image describes the output of sample program for parameterized constructors in java.

Parameterized Constructors : Output

 

Constructor Overloading : Introduction

  • Constructor overloading means defining two or more constructors within the same class. In this case the constructors are said to be overloaded and the process is called constructor overloading.
  • Now the question arises how Java determines which constructor to call . It is done by determining the type of arguments and the number of arguments in the constructor’s definition.
  • Let us take an examples to show Constructor Overloading.

 

This image describes the sample program for constructor overloading in java.

Constructor Overloading : Example

 

Output

 

This image describes the output of sample program for constructor overloading in java.

Constructor Overloading : Output