Elementary Concept of Objects and Classes

Objects

Object is a fundamental unit of Object Oriented Programming. They are referred to as entities. Objects are categorized in two ways:

  1. Real World Objects
  2. 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:

  1. It is visible to us.
  2. They have a definite shape and size.
  3. It can be brought in thoughts and figures.

Each real world object contains characteristics and behavior.

Software Objects

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.

Message Passing

Objects can interact with each other. They interact through behaviors or methods, which are better known as message passing.

Classes

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

Syntax:
<class name> <object name> = new <class name>();

Example:
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:

Class 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.

Exercises

Fill in the blanks:

  1. A class is also considered as an object factory.
  2. The objects of a class differs on various characteristics.
  3. The object of a class is represented through the attributes.
  4. The term instantiation is used for creating various objects.
  5. Class is a blueprint/template of the objects.
  6. new keyword indicates an operator for dynamic allocation.
  7. Objects are also termed as class tags or entities.
  8. Different objects of a class have common behavior.

Answer the following questions:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. Write a statement to create an object “keyword” of the class “Composite”.
    Composite keyword = new Composite();
  7. What does the following statement mean?
    Employee staff = new Employee();
    In the above statement, an object named ‘staff’ is being created that belongs to the class named ‘Employee’.
  8. Why is an object called an instance of a class? Explain.
    An object is an instance of a class because every object created has its own set of unique properties and state, even though they belong to the same class.
  9. Why is a class known as a composite data type?
    A class is more complex than the available primitive data types. It can contain both data and functions. Thus, a class is known as a composite data type.
  10. Write a statement to create an object ‘keyboard’ of the class ‘Computer’.
    Computer keyboard = new Computer();
  11. Consider a real-world object as ‘Cricket Ball’. Now mention two behaviors and methods each by taking the ‘Cricket Ball’ as a software object.
    Real world object behaviors:
    a) Spin
    b) Swing
    Software object methods:
    a) spin()
    b) swing()
  12. Refer a class structure as shown below:
    class MySchool{
    Name
    Address
    Principal’s Name
    acceptData();
    printData();
    }
    With reference to the above class declaration, indicate whether the following statements are True/False:
    (i) acceptData() is the characteristic of the class. False
    (ii) Address is the behavior of the class. False
    (iii) acceptData() and printData() are the common behavior of the objects of class ‘Our School’. False
    (iv) Behaviors are the medium of inter-object communication. True
    (v) Creating multiple objects of class ‘School’ is not possible. False

3 thoughts on “Elementary Concept of Objects and Classes”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: