Default Constructor

A constructor is called Default Constructor when it doesn't have any parameter. Syntax of default constructor:


\begin{lstlisting}
<class_name>(){}
\end{lstlisting}

In the below example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.


\begin{lstlisting}
//Java Program to create and call a default constructor
clas...
... //calling a default constructor
Bike1 b=new Bike1();
}
}
\end{lstlisting}

Output:
\begin{lstlisting}
Bike is created
\end{lstlisting}

Figure 4.4: Default constructor
Image default-constructor1

Example of default constructor that displays the default values.


\begin{lstlisting}
//Let us see another example of default constructor
//which ...
...ng values of the object
s1.display();
s2.display();
}
}
\end{lstlisting}

Output


\begin{lstlisting}
0 null
0 null
\end{lstlisting}

In the above class,you are not creating any constructor so compiler provides you a default constructor. Here 0 and null values are provided by default constructor.