Constructors in Java- Type Overloading and examples

A Constructor in a special method that is used to initialize the object.

Constructor name should be same as class name. Constructors does not have any return type.

While creating object of above class we use

Here new is used to create object and after then the constructor Student() is get called it provide initial value to object

When an object is created using new operator then to initialize the object constructor is called immediately.

A class can contain more than one constructor.  If a class does The constructor the constructor, the system provides the default constructor which is known as system default constructor resides.

The default constructor  automatically initialize all instance variable to zero

1 Characteristics of Constructor

  • The constructor has the same name as the class in which it resides.                        
  • The constructor is automatically called immediately after the object of that class created in which it resides.
  • Constructor neither returns value nor void.    
  • Access specifiers public protected,default and private can be used with constructors.                                                                         
  • The constructor can be overloaded.
  • A constructor can also call another constructor.                                                 
  • Default constructor automatically initialize all instance variable to zero  
  • Constructors can not be abstract, final ,native synchronized or static.

2 Type of Constructors

In Java Constructors are categorized in three types

  • Default (No Argument) Constructor
  • Parameterized Constructor
Type of Constructors in java

Default (No Argument) Constructor

A default constructor in no argument constructor it initialize object value to initial value.

In above program no argument constructor in provided student() on executing above program it will show following result

Result

If a java programmer has written a program and didn’t provided any constructor in program in that case during compile time compiler will include a no argument constructor.

Compiler will only include this constructor it there is no constructor available in program.

If there is already a parameterised constructor and not a no argument constructor then compiler will not include default constructor it will raise compile a time error.

Program to show use of constructor

No constructor provided in class Student. while creating object we are calling Student() (default constructor) of Student class.

The default constructor is provided by the compiler.

3 Constructors with Arguments

In Java, we can create a create a constructor which can take zero, one or more than arguments.

Result

Note: In the above program we have created class Student and three constructors  Student(), Student(long r) and Student(long r, String n ).

When the line Student obj1 = new Student();executes and immediately after the object “obj1” is create, Student() constructor will be called.When The constructor line Student obj2 = new Student (1); executes and immediately after the object “obj2” is create, Student(long r) constructor will be called and passes the value 1 to the constructor.When line “ Student obj3 = new Student(1,"Ram"); executes and immediately after the object “obj3” is create, Student(long r, String n) constructor will be called and passes the value 1 and “Ram” to the constructor.

4 Java Constructor Overloading

Like a method’s, we can create a more than one constructor with same name as the class name.

But each constructor has a different parameter. Such constructor is dfcalled overloaded constructor and this procedure is known as constructor overloading.

Syntax

Example

Explanation: In the above program inside class “Employee”, we have created five constructors and each constructor have the different parameter list.

Such constructors are called the overloaded constructor and the process of creating multiple constructors with the same name with different parameter list called constructor overloading.