It is a paradigm or approach which involves organization of data and related functions in a single entity called object, and the objects having similar properties are grouped together, we define their blueprint so to say in what is called a class.
In other words,
Data and related Methods(Functions) are wrapped inside a single entity called object, which showcases a property of OOP called encapsulation.
But before this wrapping happens, an object is to made aware about what it can consist so that it can allocate that much memory for itself.
So this process of making an object aware about what it will consist even before it has been declared is done through a class, which can be thought of as a blueprint for objects which will have those particular specifications.
A class is a logical entity whereas an Object is a physical entity. What it means is, that when a class is defined it acts as the structure for an object, the data it will contain and the methods it will have.
It doesn’t reserve any memory space as it is just the blueprint of what objects of that class will be like and contain.
And also a definition of class is necessary before the declaration of an object because then only the compiler will know how much memory is to be allocated to a particular object.
In this regards, classes and objects can be thought of as data types and variables.
For example mere existence of data type “int” doesn’t create any variable or reserves any memory space so is the case with the definition of a class.
Any variable of type “int” will occupy the same memory space so will the objects of a same class.
An object simply can be anything, we just need to think with respect to two things, attributes(data) and its behavior(method).
Think of a scenario in a college where you have multiple courses to opt for,
Suppose 40 students take up math course and 80 students including those 40 who took math take computer course.
The very first thing to do here is to identify all the entities we can see,
In this example we see three major things
- Math Course
- Computer Course
Since Math Course and Computer Course have fundamentally same properties.
We can narrow it down to two,
- Math Course/Computer Course.
Now we need to ask this question to ourselves that which is the entity which has some attributes and is doing some work ?
Now the next step would be to identify the attributes and behavior of each entity or in other words to find the answer of two questions ,
- What it has?
- What it does?
Examining our entities,
Q) What it has? -> Name, Unique Roll Number, Age to name a few.
Q) What it does?-> Takes up different courses, Studies them.
Q) What it has?-> Unique course id, Course duration to name a few.
Q) What it does?-> The course itself does nothing, it has some content which the teacher teaches to the students.
So here student is the physical entity and can be treated as an Object.
But before that we need to have a structure defined for these objects somewhere,
As from the statement in the example we can see that 40 students have selected one course and 80 have selected another course including that 40,
So in place of defining course for each student separately which is what we would have done in traditional programming paradigm we will combine/group the students having similar course.
We will define a class named “Math” for students taking up math and class named “Computer” for students taking computer.
What it will allow us to do is to encapsulate different entities and also to extend them easily.
And moreover since we have a special case here, we can inherit Math class in our Computer class as those 40 students are the same.