Constructor in java

What you will learn here about constructor in java

  • What is  constructor in java
  • Types of constructor in java
  • Default constructor in java
  • Parameterized constructor in java
  • Constructor overloading in java
  • Can we override constructor in java

What is  constructor in java

The constructor is very much similar to the method but it doesn’t have any return type, not even void. The constructor is used to initialize the instance variables at the time of object creation. Variables that are defined inside the class and outside the methods are called as the instance variable.

1)Constructor name is always the same as the class name.
2)Constructor does not have any return type, not even void.
3)Constructor can be overloaded
4)Constructor can not be inherited
5)Constructor can not be overridden because you can not inherit the constructor.
6)Constructor gets automatically called whenever we create an object.

Types of constructor in java

There are two types of constructors in java and those are :

  1. Default constructor
  2. Parameterized constructor

Types of constructor in java

Default constructor in java

The constructor which is provided by the compiler is called as default constructor. The default constructor is used to initialize the instance variables to a default value at the time of object creation. Whenever we create an object by default, the default constructor gets called which shown in the below example. The access modifier of the default constructor is the same as the class access modifier. If the class access modifier is public then the default constructor access modifier is also public.
Default constructor in java

Parameterized constructor in java

The constructor which is not provided by compiler or constructor which is written by the user is called a parameterized constructor or custom constructor.  Parameterized constructor used to initialize the instance variables to the custom value or user defined value at the time of object creation. The access modifier of the parameterized constructor can be anything, it need not be necessary to the same as access modifier of class. In the below example constructor with no input parameter is also a parameterized constructor as it’s written by the user. The Custom or Parameterized constructor with no input parameter behaves the same as the default constructor. which is shown below example.
Parameterized constructor in java
In the below example of custom or parameterized constructor, instance variables are initialized to custom or user defined value. The user needs to pass the custom values at the time of object creation to custom or parameterized constructor which is shown below.
Parameterized constructor java

Constructor overloading in java

In java, constructor overloading is possible. The overloading of constructors in java is shown below.
Constructor overloading in java

Can we override constructor in java

No, you can not override the constructor because the constructor can not be inherited.

You may also like...

Leave a Reply