Objects and classes are the building blocks of OOP. To understand OOP, first we have to know what objects and classes are.
An object is a programming entity in OOP. It has data components representing the present state of the object as well as functions (also called methods). Data components are termed attributes in the context of OOP languages. An area of memory is kept aside to store values for the attributes when an object is created. Functions describe the behaviour of the object and allow the other programming entities to interact with the object.
An object contains attributes (variables) and methods (member functions). Attributes are the data associated with the object and the methods are the functions and code which operate on the data.
A class is a model to define objects. In other words, an object is said to be an instance of a class. An object may be real or abstract. That is, objects can model concrete things such as people, and data entry forms as well as abstractions such as numbers, equations or geometrical concepts.
Consider an example of a class—car. The class car describes all car objects by specifying the data components and the methods that each can have. Various objects defined in the class car could be Maruti 800, Zen, Santro, Accent and so on. The class car has various attributes such as the seating capacity, maximum speed, engine capacity, parking light, current speed and current position. Typical functions of the car class are to find acceleration and set parking lights on. See Figures (a) and (b) for an illustration.
Real-world objects have two characteristics: state and behaviour. For example, consider an object ball. A ball has particular characteristics such as its diameter, colour and elasticity. Formally, we say that these characteristics contribute to the ball’s state of being. We also describe a ball by what it does, such as the fact that it can be thrown, bounced or rolled. These activities define the ball’s behaviour, similarly the object car has state (colour, wheels, current gear, number of gears and so on) and behaviour (braking, changing gears, accelerating, decelerating and so on).
Software objects are modelled after real-world objects in that they too have state and behaviour. Attributes define the state of the object and methods define the behaviour of the object. That is, a software object maintains its state in one or more attributes. Attributes can have different values at different instances of time; however, values of a particular attribute must have the same data type.
An attribute/variable is an item of data named by an identifier, and a software object implements its behaviour with methods. A method is a function/sub-routine associated with an object.
The state of an object and its behaviour work together. For example, how high a ball bounces depends on its elasticity. An object’s behaviour often modifies its state. For example, when a ball is rolled, its position changes.
An object must also have a unique identity that distinguishes it from all other objects. Two objects can have the same data types of attributes with the same names, and those attributes can have the same values. Therefore two objects could have the same state. There must be a way of distinguishing the two objects and that is done by using an object’s name. In a program, to define an object we need a class.
A class is a model or pattern from which an object is created. In other words, a class is a special module that defines an object.
A class defines the data types of attribute that will be held in an object, and defines code for the methods. After a class was defined, an object can be created from it. The process of creating an object from a class is called instantiation. Every object is an instance of a particular class.
Defining a class is having a name, a set of attributes and a set of methods defined. The attributes will not have any values associated with them when a class is defined; however, when an object is created from this class, the attributes of this object will have values. The values of the attributes .can be accessed and/or modified by the methods of that class and cannot be accessed or modified by the objects of the other class.
The definition of a class must not be confused with that of an object. A class is the outline for an object. But a class is not an object any more than a blueprint. We introduce the notion of data types, which makes easy to understand the difference between objects and classes. Consider a data type integer. We can declare variables to be of type integer. For example, in C, we can do this with the statement
int i, j;
This statement defines two variables i and j of type integer. When this statement is executed, the two variables are created. Similarly, an object can be defined by a class. That is, a class is similar to a data type (data structure) and an object is like a variable created from some class. In other words, a class defines a type and an object is a particular instance of the class. Suppose Circle is a class, the objects A and B can be created from that class, as in C++, where A and B may be circles of different diameters.Circle A, B;
An object can be viewed from two perspectives. The first is useful during design and implementation of the object. We have to define the attributes that will be held in the object and write the program code for the methods that make the object useful. The second comes in when making use of the services of an object that was already created. When designing a solution to a large problem, we should know only the services that an object provides rather than the details of how the services are accomplished. That is, from the second point of view, an object is a black box. It is a part of a system whose inner workings and structure are currently hidden. The rest of the system only interacts with the object through a well-defined set of services (called messages) that it provides.
In OOP, objects communicate with one another by sending and receiving information using messages. For an object, a message is a request for execution of a method. When a request is made to an object, it invokes the specified method in the receiving object, then the receiving object responds by generating the desired result. Message passing involves specifying the name of the object, the name of the method and any information that needs to be sent.