Abstract
Method signature
- The name of the constructor must be the same as the name of the class
- Constructors do not have a return type, not even
void
- Constructors can have access modifiers
Calling superclass's constructor
super(..)
must be the first line inside the subclass’s constructor. This means that if your call tosuper(..)
requires a computed value, the value must be computed inline as arguments tosuper(..)
.
Java Default Constructor
- Java’s default constructor is called implicitly when you don’t define any constructors in your class.
Properties
- Doesn’t set any fields
- Calls the super constructor, the default super constructor
Chaining Constructors
- Instead of maintaining two separate constructors,
Circle(Point c, double r)
andCircle()
, we only need to maintainCircle(Point c, double r)
. This is achieved by chaining the implementation ofCircle()
toCircle(Point c, double r)
usingthis(new Point(0, 0), 1)
.
this()
The call to
this(..)
(if used) must be the first line in the constructor.You can’t have both
super(..)
andthis(..)
in the same constructor.If you use
this(..)
, the default constructor isn’t automatically added.