A POCKET GUIDE TO OBJECT-ORIENTED PROGRAMMING (OOP)

Constructors

As we have already discussed in our chapter for Classes that a class is a definition, template or blueprint of objects of same types.

And we have also discussed about Objects that are instances of a class and can be created or initialized.

Than we also read and discussed about Properties and Instance variables in our last chapter. And we also saw a visual representation of this concept.

You might be thinking that if Object is a instance of a class that can be created or initialized and also it can have one or more instance variables, than how can we initialize Objects and assign values to its instance variables?

The answer is ‘Constructors’

A Constructor is basically a special function that is used to create an object of that class.

We have already discussed about functions in our programming core course. And constructors are also functions. But, what is so special about these functions.

There are two main points that make constructors so special. First, these are used to initialize class objects. And there is no other way to do this in most of the OOP languages.

Second, a constructor must be named exactly as name of that class. What is this? If we take our class Product then constructor of this class must also be named as ‘Product’. Similarly, class Human will have constructor name ‘Human’, class Automobile will have ‘Automobile’, and so on.

Constructor Types

A class can have one or more constructors and it depends on our choice that how many different ways we need to initialize objects of that class. But, there are usually three very basic and common types of constructor.

1. Parameterized Constructor

As the name suggests these are constructors that take or accept one or more arguments or parameters.

For example our Product class can have a parameterized constructor which will only take one argument for Name. It can also have one more constructor that will take equal number of arguments as class properties.

2. Default or Empty Constructor

As a class must have at least one constructor, that’s why, we can declare a constructor with empty arguments. This constructor will be known as default or empty constructor.

Different programming languages have different standards for this. For example Java will automatically create a default constructor if we don’t declare at least one constructor for a given class. While C++ will force you to declare a default constructor, that can be used to create empty or temporary objects.

3. Copy Constructor

Sometime you need to create a copy of an object. You can use a parametrized constructor for this. But it would be better if we can just pass a source object as constructor argument. This constructor will be known as a copy constructor.

It will just take one object and return or initialize another object with same values of instance variables of object that will be passed to that constructor.

So if we summarize or visualize these three common types of constructors, it will look like this.

With this, we can conclude our discussion on Constructors. And I believe you have understood this concept now. If you still have any confusion on this, please ask your questions as comments below.

Let’s move on to our next chapter. Please click on ‘Next’ to continue.

Image placeholder

Hi! I'm Zeeshan Elahi

I have compiled and prepared this content with love for people like me and you. And as you know it always take some time and extra cups of coffee to read, compile and prepare a course or manual. If you have like reading this content and want to say thanks, please consider supporting from more stuff like this.

Leave a comment

Your email address will not be published. Required fields are marked *

*

*