It is possible to define more than one constructor in a program, which is known as Constructor Overloading or multiple constructor.

 

Example

class Example
{
int x,y;
Example()
{
x = 0;
y = 0;
}
Example(int z)
{
x = y = z;
}
Example(int p, int q)
{
x = p;
y = q;
}
void show()
{
System.out.println(x+" "+y);
}
}
class TestExample
{
public static void main(String[] args)
{
Example obj1 = new Example();
Example obj2 = new Example(5);
Example obj3 = new Example(2,3);
obj1.show();
obj2.show();
obj3.show();
}
}
Output

0 0
5 5
2 3

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

Silansoftware Pvt.Ltd, BBSR
Call to: 0674-2361252

Comment