DEV Community

rachana20
rachana20

Posted on

A Class (not a collection of objects)

One day I got a call from my friend asking me the meaning of a class for which I instantly answered that the class is a ‘collection of objects’. Well, you know what I was wrong, this lead me here to tell my fellow mates that class is not a collection of objects.
Before going into programming meaning of a class, let’s see what a class is in real world.
A class is a set of things having some property or attribute in common, for example, apple, orange, grapes known as fruits in general but has its own identity, attribute and behavior.
Similar in programming too, Class is a template/blueprint to create objects, which are instance of that class used in object-oriented programming. Well, everyone has this assumption of class being a collection of objects which is totally wrong because an object can be instantiated through class and with one class you can create many objects of same type thus an object becomes instance of a class but not the other way round. There are many data structures designed to hold a collection of objects such as arrays and lists but definitely not class.
This example gives you a clear picture on what a class is and why It cannot be an object.

Image description

A class has some set of rules called as methods and some values known as data members. Both data and methods are defined automatically in a class when an object is created. Hence one can make as many objects as they want based on class. In the above example Human (the template) is created first and then the objects of human can be created based on the template and each object must have the data and methods.

Top comments (0)