Object
An object is the software representation of a real-world entity. For example, objects in a railway reservation system may include a train, a journey destination, an icon on a screen, or a full screen with which a railway booking agent interacts. Any tangible entity in the real-world can be described as an object. Example of real-world entities around everyone, such as a car, bike, flower or person.
Every entity has some characteristics and is capable of performing certain actions. Characteristics are attributes or features describing the entity, while actions are activities or operations involving the object.
For instance, consider a real-world entity such as a Toyota or a Ford car. Some of the characteristics associated with a Toyota or Ford car are:
Some of the actions associate with a Toyota or Ford car are:
Drive
Accelerate
Decelerate
Apply brake
Software Object
The concept of objects in the real-world can be extended to the programming world where software “objects” can be defined.
Like its real-world counterpart, a software objects has state and behavior. The “state” of the software object refers to its characteristics or attributes whereas the “behavior” of the software object comprises its actions.
For example, a real-world object such as a car will have the state as color, make a model and the behavior as driving, changing gear and increasing speed, applying brakes.
The software objects stores its state in fields (also called variables in programming languages) and exposes its behavior through methods (called functions in programming languages).
Defining a class
In the real-world, several objects having common state and behavior can be grouped under a single class. For example, a car (any car in general) is a class and Toyota car (a specific car) is an object or instance of the class.
A class is a template or blueprint which defines the state and behavior for all objects belonging to that class.
Typically, in an object-oriented language, a class comprises fields and methods, collectively called as members, which depict the state and behavior respectively.
comparison
Class | Object |
Class is a conceptual model | Object is a real thing |
Class describes an entity | Object is the actual entity |
Class consists of fields (data members) and functions | Object is an instance of a class |