Types of Constructors

In this topic, you will learn about, Types of Constructors.

Constructor: It is a special member function of a class used to initialize object members. Constructors use the class name for declaration and cannot specify any return type even void. It must be declared publically and it’s called automatically when an object creates.

There are two types of constructors used with java language.

Parametrized: It is also known as overloaded or argument constructor. parametrized constructor called when any object specifies value at the time of creation. There are two types of parameterized constructors.

  • copy constructor
  • Overloaded constructor

Where overloaded is used to create multiple constructors in a class but we can specify a different argument for each constructor. Copy constructor use class type argument and its call when any object initializes with another object.

Non-parametrized: It is also called the default constructor. Java language provides a default constructor automatically or we can also create our own default constructor.

Syntax:

public <class name>(argument)
{
Statement;
Statement;
}

Eg:- class abc

{
Int a,b,c;
public static void main (string arg[])
{
abc obj=new abc (); //call default(compiler) constructor
}
}
Class circle
{
Float PI ,r ,area;
Public circle();
{
PI=3.14;
R=area=0.0f;
}
Public static voidmain(string arg[])
{
Circle obj = new circle ();
}
}

Comment below if you have queries related to the above topic, Types of Constructors.

See also  JAVA - Applet Program to Demo Dialog Box