Object is a fundamental unit of Object Oriented Programming. They are referred to as entities. Objects are categorized in two ways:
- Real World Objects
- Software Objects
An object is also called an instance of a class.
Real World Objects
These objects are the ones that we experience in our day-to-day life. Following are some of its features:
- It is visible to us.
- They have a definite shape and size.
- It can be brought in thoughts and figures.
Each real world object contains characteristics and behavior.
It is an object that is created while writing a Java program. It contains data members and methods.
The state or characteristics of the real world objects are considered to be data members in the software objects.
The behaviors in the real world objects are considered to be the methods in the software objects.
Objects can interact with each other. They interact through behaviors or methods, which are better known as message passing.
A class is a blueprint or template for its objects. It is simply a representation of similar types of objects.
Creating objects of a class
<class name> <object name> = new <class name>();
Car bmw = new Car();
The new keyword is used to allocate space in the dynamic memory for creating an object.
Attributes are characteristics or data members of an object.
The state of an object is represented by the attributes of that object.
Important terms related to classes
- Class is an object factory.
- Class is a user defined data type.
- Object is an instance of a class.
Differences between a class and an object:
|It is a representation of an abstraction.||It is a real and unique entity.|
|It is an object producer.||It is created using “new” operator.|
|It is known as an object factory.||It is known as the instance of a class.|
Fill in the blanks:
- A class is also considered as an object factory.
- The objects of a class differs on various characteristics.
- The object of a class is represented through the attributes.
- The term instantiation is used for creating various objects.
- Class is a blueprint/template of the objects.
- new keyword indicates an operator for dynamic allocation.
- Objects are also termed as class tags or entities.
- Different objects of a class have common behavior.
Answer the following questions:
- Class and objects are interrelated. Explain.
A class is the blueprint for an object, whereas an object is an instance of a class. Thus, classes and objects are interrelated.
- A class is also referred to as an object factory. Comment.
With one class, we can create several objects of similar kinds. Thus, a class is also referred to as an object factory.
- What does the following statement mean?
Employee staff = new Employee();
In the above statement, we are creating a object named “staff”, of class Employee, using the new operator.
- Why is an object called an instance of a class? Explain.
When we create several objects from a class, each object gets unique properties of their own. And these objects actually exist in the dynamic memory of the computer. Thus, an object is called an instance of a class.
- Why is a class known as composite data type?
A class is known as a composite data type because classes are made up of primitive data types. They are more complicated than primitive data types. They contain both characteristics and behavior.
- Write a statement to create an object “keyword” of the class “Composite”.
Composite keyword = new Composite();