Constructor Overloading

Constructor Overloading.
Definition : Can Create as many constructor in our class. Constructor overloading can differ in parameter lists in class. The compiler taking into account the number of parameters in the list and their Type.

package javaconstructor;
/*
* @description
* – Constructor overloading is just create another constructor.
* – same name as of class but different signature (parameters).
* – Compiler differentiates which constructor is to be called depending upon the
* number of parameters and their sequence of data types.
* @author Frank Nandong
*/
public class cons_overloading {

public cons_overloading()
{
System.out.println(“Constructor default”);
}
public cons_overloading(int x)
{
System.out.println(“Circle perimeter: ” + 2*Math.PI*x);
}
public cons_overloading(int x, int y)
{
System.out.println(“Rectangle perimeter: ” +2*(x+y));
}

public static void main(String args[])
{
cons_overloading p1 = new cons_overloading();
cons_overloading p2 = new cons_overloading(10);
cons_overloading p3 = new cons_overloading(10, 20);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s