Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Constructor Overloading

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

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext