The concept of Class and Object is related to OOP, and It is a bit difficult topic to understand for beginners. But, here I will try to simplify these two concepts.
So before proceeding, you should know what is OOP (Object-oriented programming)?
What are Classes in OOP?
OOP is used to write programs in an organized way, and to organize programs using OOP we group all of the functions and variables according to the particular topics.
For example, we have a School management system and in this application, we have Student related functions and variables, and Teacher related functions and variables.
To organize the code we will separate all the functions and variables of the application into two groups – 1) Teacher, and 2) Student.
But, if you find that the Teacher and Student have some similar functionalities, you can group those similar functionalities into another group to reduce code.
Here comes the
Classes for grouping. So a Class is a collection of methods (functions) and properties (variables) of a particular topic.
What are Objects in OOP?
Let’s move on to how to use the methods and properties of a class –
You cannot directly call a method of a class. In order to use the methods and properties of a class, you must first create an instance of the class.
With the help of a class instance, you can access (use) the methods and properties of the class.
These class instances are called objects.
You can create multiple instances of a class, and each instance will be different from the other (which means every object has its own memory), and that way you achieve the code reusability.
What are Methods and Properties?
In simple words, a function created inside a class (or you can say a function associated with an object) called a method, and likewise a variable defined inside a class called property.