Constructor

Let's discuss constructor in this lesson.

Introduction to a constructor

Constructor is a special method that is automatically called when an object is created. There are certain syntactical rules when creating a constructor.

  • The constructor has the same name as that of the class.

  • Every class must have a constructor. Provided that a constructor is not defined, the compiler will create a default constructor which will be empty!

Let’s look at an example of a constructor in the code snippet below.

Get hands-on with 1200+ tech skills courses.