Elementary Concept of Objects and Classes
ICSE Class 9 · Computer Applications · Chapter 2
What this chapter covers
- Modelling entities and their behaviour by objects
- State and behaviour of an object
- Class as a specification for objects
- Class as a blueprint for objects
- Class as an object factory
- Computation as message passing between objects
- Class as a user-defined data type
- Difference between a class and an object
Key points
- An entity is any real or imaginary thing that can be clearly identified and described; an object is its software model in a program.
- Modelling keeps only the details and actions the program needs, which is abstraction applied to one entity.
- Every object has an identity, a state and a behaviour.
- The state of an object is the set of values of its attributes at a moment; its behaviour is expressed through its methods.
- Attributes are also called data members, member variables or instance variables; methods are also called member methods.
- Objects encapsulate state and behaviour: data and methods live together, and the data changes only through the object's own methods.
- A class is a specification for a group of objects that share the same attributes and behaviour; it is not itself an object.
- A class is a blueprint: one design, many separate objects, each with its own values.
- A class is an object factory: it can produce any number of instances, and creating one is called instantiation.
- Objects work by message passing: one object calls a method of another, naming the receiver, the method and any arguments, as in `fan.setSpeed(4);`.
- A class is a user-defined, composite, non-primitive data type with its own functionality, used to declare variables just like `int`.
Questions answered
Define an object. What three things does every object have?
An object is a software entity that represents a real-world entity in a program and combines the data describing that entity with the actions it can perform. Every object has an identity, a state and a behaviour.
What is meant by the state of an object? Give an example.
The state of an object is the set of values held by its attributes at a particular moment. For a ceiling fan object, the state "on = true, speed = 3" describes the fan at that moment.
Distinguish between attributes and methods.
Attributes are the characteristics that describe an object and are stored in its variables (data members), such as the balance of an account. Methods are the named blocks of code that express the object's behaviour, such as `deposit()` and `withdraw()`, and they usually read or change the attributes.
Why is a class called a blueprint for objects?
Like an architect's blueprint, a class lays down the design (the attributes and methods) that every object will have, but it is not itself an object. Many separate objects, each with its own values, can be built from one class, just as many houses can be built from one blueprint.
Why is a class called an object factory?
A class can produce any number of similar objects whenever the program needs them. All the objects it produces have the same attributes and methods, but each holds its own values, just as a factory produces many similar items from one design.
What is an instance? How is it related to a class?
An instance is a particular object created from a class, holding its own values for the attributes the class specifies. Every object is an instance of the class from which it was created.
Explain message passing with a real-life example.
Message passing is the way objects communicate: one object asks another to perform one of its methods, giving any values it needs. For example, a remote object sends `changeChannel(5)` to a television object, and the television changes its own channel to 5 without the remote knowing how it does so.
Identify the receiver, the method and the argument in `account.deposit(500);`.
The receiver is the object `account`, the method is `deposit` and the argument is 500. The message asks the account object to add ₹500 to its own balance.