Classes and Objects. All of the programming languages which…
Tags: Programming, Class, Object Oriented, Objects, Constructor

All of the programming languages which have Object-Oriented Programming (OOP) feature have classes and objects. We can think of a class as the blueprint for creating objects.

To understand them, let’s see the real-world example:

We have an Animal class, and Cat object which is the object of the Animal class. Animal class has some specifications like their color, height, average life expectancy and so on. If we create an object of it with the name of Cat , then it will have all of those specifications.

Similarly, if we look at the Object-Oriented Programming, we can see that a class defines some fields, properties, methods etc. These are properties which the objects of this class will have.

By definition, a class is the user-defined data type which is created by grouping built-in data types, methods, constructors etc.

As we understood the basic meaning of classes, let’s dive into the programming to learn how are they used.

Note: In this tutorial, I am using C# language, but it does not matter which language you know. The idea behind this blog is understanding classes so that you can implement in whatever language you want.

Firstly, we need to declare classes outside of Main method (In some languages like Python, you do not have Main method, so you simply need to declare it):

To declare a class we use the following syntax:

[access modifier] class [unique name] {}

For example:

Note: In this case, we did not specify the access modifier so that it will be private automatically. So, it will not be accessed outside of Program class.

We can declare variables and methods in that class:

As you see, we declared an int variable age, a string variable name, and an Eat() method. So, now the Animal class have properties. When we create objects, they will be able to use those variables and methods.

Note: It is not good practice to make variables public, because it gives direct access which can cause problems. For now, to make the example simpler, I marked them as public.

If we review what we have done so far, first we declared a class, then declared its variables and methods. Now, let’s see how we create an object of the class:

To create an object of a class, we first declare it then we initialize it:

When we initialize an object, it automatically calls a constructor. To understand how these all work, let’s first learn constructors.

What are Constructors? How do we use it?

As the name implies, the constructors are used to construct a class. We can have so many constructors. Similar to methods, they may have parameters.

Their syntax is like this:

[access modifier] [class name] () {}

A constructor should be public to create an object by using that constructor.

Constructors are similar to methods, but they are entirely different things. They do not have any return type. They have different versions, but in our case, we only need to know default and parameterized constructors. Because we need to learn them to understand how objects are created.

So, let’s first have a look at the Default Constructor:

In this code, We created a default constructor which assign 5 to the age and “Cat” to the name. It does not matter how many objects we create, if we create that objects using this constructor, then its age will be equal to 5, and name will be equal to “Cat”.

Parameterized Constructors

When we create an object, we can pass some parameters like an age to the constructor, and assign those parameters to the object’s variables. These types of constructors are called parameterized constructors To do that, we need to create a parameterized constructor:

Let’s understand those codes, one-by-one:

Note: In this case, we are not using default constructor, so its existence will not matter for us. I just did not delete it, because I want to show two or more constructor can be used in the class.

Firstly, we created our second constructor which takes two parameters:

an integer age

a string name

Then, it assigns those values to the object’s values in this part:

this.age = age;

this.name = name;

By using this keyword we can differentiate the class’s age or name variables and the ones which are passed to the constructor.

To review what we have done here:

We first initialize an object with two parameters 10 and Dog corresponding to age and name.

corresponding to Then we assign those values to the object’s age and name values

In this case, we can use that object to access its variables and methods:

As you can see, we created an object with the name of dog and accessed its properties:

We printed its age

We printed its name

We called its Eat() method which prints “Food has been eaten”;

After all of these statements, the result will look like this:

Creating an object from a class in which we did not create a constructor

Till now, we have learnt that when we want to create an object of any class, we have to use constructors.

But a question arises, how can an object be created if we did not have a constructor in a class.

The answer is simple. If we do not create any constructor, it automatically does it behind the scene. For example:

In this code piece, we did not declare a constructor, but we want to create an object of that class. As you can see we write:

Animal animal = new Animal();

In this case, the Animal class will say to the animal objects that:

Class: Hey, I do not have any declared constructor, so I will initialize you with my default constructor.

Then the object will automatically be initialized. We do not see that constructor, but we know it is there and works behind the scene. This constructor is called default constructor.

Creating More Than One Object

In the previous examples, we created only one dog object. Now, let’s see how we are creating multiple objects:

Our Animal Class:

In the Eat() method I did some changes so that when it is called it will print in the format "Animal ate the food” . The rest of the class is the same.

Our Main method which creates 3 objects:

In this case, as you can see we are creating 3 objects, printing their age , name , and calling their Eat() method. At the end, the result will look like this:

Firstly, it prints dog’s information, then cat’s, and at the end, it prints bird’s information.

Conclusion

To summarize, Classes and Objects are the most necessary topics in Object-Oriented Programming. Without learning them, we cannot continue to OOP.

In this article, I have explained classes and how are they used.

I hope it was helpful and you liked it.