Javatpoint Logo
Javatpoint Logo

Two constructors in one class in Java

A class may include several constructors. The constructors' parameter lists, however, shouldn't be identical. Overloading of constructors is what this is called.

It is demonstrated by the following program:

ConstructorExpl.java

Output:

Two constructors in one class in Java

Let's now examine the aforementioned software.

The data member num and the single member function display() that shows the value of num are the building blocks for the NumberValue class. The class NumberValue has two constructors, one of which accepts no parameters while the other of which accepts a single int parameter. The following code snippet illustrates this:

The display() function is called on the objects o1 as well as o2 of the class ConstructorExpl that are created in the main() method. Here is a sample of code that illustrates this:


Next TopicUnion in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA