java

Lessons learned on object constructors

Constructors are a basic building block of object-oriented programming (OOP). They expose ways to build specific types of objects consistently, using arbitrary rules to validate properties. Still, constructors are odd beasts in the OOP world. In Java, this is usually the first case of function overloading that learning programmers meet, often without knowing the term. An overloaded constructor is shown in the following example: class Car { private Motor motor; public Car(Motor m) { this.