What are objects?
new, can be considered an object.
What Is an Object?
In real life, objects are found everywhere, so these real-life scenarios can also be mapped into object-oriented code.
Let’s discuss this by looking at an example. Assume you have three shapes: square, rectangle and circle. Now, you want to calculate the areas of all three shapes.
If you had to write a piece of code that would calculate the areas of these three shapes, what would you do? A common approach would be to write functions for all three shapes to compute the respective areas.
Another approach would be to convert our code to the object-oriented paradigm by creating objects for each shape: square, rectangle, and circle. Here, each object has its own set of properties which include:
- data values
In our example, what values would we need to calculate the areas of the three shapes?
We need the length, width, and radius. These values will be encapsulated in the object of that particular shape.
Similarly, we also require a function to calculate the area. This will be encapsulated in the object as a part of its properties.