Does java support constructor with default valued parameters e.g.
public Shape(int v=1,int e =2){vertices =v;edges = e; }
No, Java doesn't support default values for parameters. You can overload constructors instead:
public Shape(int v,int e) {vertices =v; edges = e; }
public Shape() { this(1, 2); }
No it doesn't. Java doesn't support default arguments in any function; constructors included.
What you can do though is define public Shape(int v, int e)
and also a default constructor
public Shape()
{
this(1, 2);
}
Note the special syntax here to delegate the construction to the two-argument constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With