Constructor is a function which called automatically
when a object of class is initialized,
But a method is called when its explicit called by object
constructor not have any return type
Normal Function has.
use of constructors is, we can feed the default values which we need to pass to that class
public class Class1
{
private int a, b;
public Class1(int a, int b) //Constructors with parameter
{
this.a = a;
this.b = b;
}
public Class1() //Constructors without parameter
{
}
}
Main()
{
Class1 cls = new Class1();
Class1 cls1 = new Class1(2, 3); // Here we can feed Default values to a class
}
A constructor can be used to set default values