Constructor chaining

Updated: 08/08/2017 by Computer Hope
constructor chaining

In object-oriented programming, constructor chaining is the technique of creating an instance of a class with multiple constructors, then using one constructor to call another. The primary use of constructor chaining is to make a program simpler, with fewer repeated lines of code.

How does it work?

In a language like Java, a class can be thought of as a template for structured data, where all characteristics and actions defined generally. An instance of the class is a specific data structure with unique values in this defined structure.

Normally, a class has a special method that shares the exact name as the class itself. This method, called a constructor, is executed automatically when a new instance of the class is created. Using ad hoc polymorphism, multiple constructor methods can share the same name and call each other, giving the programmer more options as to how an instance might be created.

Object-oriented, Polymorphism, Programming terms