In every first lesson of object-oriented programming, the concepts of classes and objects are introduced. Typically,
- an object represents an entity in real life that has states and behaviours, and
- a class is a blueprint/template/design of a certain type of similar objects.
When writing a function, we are defining what will happen when the function is used (invoked). For example, let's define a function sum
:
This definition does nothing more than allowing the system to look up the method's execution when it is used elsewhere. When this function is invoked
sum
and executes the code in the definition.
In a similar way, a class is the definition of an object. It defines what will happen when the object is used. While using a function is mostly obvious - invoking it, using an object can be more complex - creating it, accessing its data fields, invoking its methods. Here I write a class Circle
(as definition of Circle
objects):
When Circle
objects are used in some other code, the system will look into the class and execute the proper code in the class. For example, with
Circle
will execute and a Circle
object is created. With
radius
of the newly created Circle
will be changed. With
getRadius
of the Circle
will be executed.