Class and Object

Comprehensive study notes, diagrams, and exam preparation for Class and Object.

Class and Object

Definition

A class is a user-defined blueprint or template that describes the properties and behaviors of an entity. An object is a real instance of a class, created using that blueprint, which has its own data values and can perform the methods defined by the class.


Main Content

1. Class

  • A class defines the structure of data and functions together. It is not the actual thing itself; it only describes what an object should look like and what it should do.
  • A class usually contains data members (also called attributes or fields) and member functions (also called methods) that operate on those data members.

Example idea:

  • Class: Car
  • Data members: color, brand, model, speed
  • Methods: start(), stop(), accelerate()

A class can be understood as a blueprint for making many similar objects. Just as a house plan can be used to build many houses with different colors or sizes, a class can be used to create many objects with different values.

Important characteristics of a class:

  • It is a logical entity, not a physical entity.
  • It is created once and can be used to create many objects.
  • It supports encapsulation by grouping data and functions together.

ASCII representation for understanding class concept:

        Class: Car
   -----------------------
   | Data Members        |
   | - color             |
   | - brand             |
   | - speed             |
   -----------------------
   | Methods             |
   | + start()           |
   | + stop()            |
   | + accelerate()      |
   -----------------------

This means the class contains the structure and the actions that the objects of that class will have.


2. Object

  • An object is an actual entity created from a class. It has a real existence in memory and holds actual values for the data members defined by the class.
  • Each object can have its own state and can call the methods of the class to perform operations.

Example: If Car is a class, then objects can be:

  • car1 = red Toyota
  • car2 = blue Honda
  • car3 = black BMW

Although all these objects belong to the same class, each can have different values for color, brand, or speed.

Important characteristics of an object:

  • It is a physical entity because it occupies memory.
  • It represents a specific instance of a class.
  • Different objects of the same class may have different states.
  • Objects interact with one another through method calls or messages.

ASCII representation for object concept:

Object 1                Object 2
--------                --------
color = red             color = blue
brand = Toyota          brand = Honda
speed = 60              speed = 80

This shows that both objects are created from the same class but store different information.


3. Relationship Between Class and Object

  • A class and object are closely connected: the class creates the definition, and the object uses that definition to become a usable entity.
  • One class can generate many objects. All these objects share the same structure and methods, but each object has its own values.

The relationship can be understood like this:

  • Class = blueprint
  • Object = actual house built using the blueprint

For example:

  • Class: Student
  • Attributes: name, roll number, marks
  • Methods: display(), updateMarks()
  • Objects:
  • student1: name = Asha, roll number = 101
  • student2: name = Rohan, roll number = 102

Both objects belong to the same class but represent different students.

Key relationship points:

  • Class defines.
  • Object uses.
  • Class is abstract in nature.
  • Object is concrete in nature.
  • Objects are created from classes, not vice versa.

Simple diagram showing the relationship:

          Class
      ---------------
      | Student      |
      | name         |
      | rollNo       |
      | marks        |
      | display()    |
      ---------------

            |
            | creates
            v

   -------------------   -------------------
   | student1 object |   | student2 object |
   | name = Asha     |   | name = Rohan    |
   | rollNo = 101    |   | rollNo = 102    |
   -------------------   -------------------

Working / Process

1. Define the class

  • First, a class is written with required data members and member functions.
  • The class describes what type of data the object will store and what operations it can perform.
  • Example: define a BankAccount class with accountNumber, balance, deposit(), and withdraw().

2. Create the object

  • After defining the class, one or more objects are created from it.
  • Each object gets its own memory space and stores actual values.
  • Example: acc1 may have balance 5000 and acc2 may have balance 12000.

3. Use the object to access data and methods

  • The object is used to access the class members.
  • Methods change or display the state of the object.
  • Example: calling acc1.deposit(1000) increases only acc1’s balance, not acc2’s.

Example flow:

Class defined -> Object created -> Methods called -> Output/Result produced

Illustration:

BankAccount class
   |
   v
acc1 object --------> deposit() --------> balance updated
acc2 object --------> withdraw() -------> balance updated

This process shows how a class becomes useful only when objects are created and used.


Advantages / Applications

Encapsulation of data and behavior

  • Class and object help combine data and related functions into one unit.
  • This makes programs cleaner and reduces complexity.

Code reusability

  • A single class can create many objects, so the same code can be reused repeatedly without rewriting logic.

Real-world modeling

  • Classes and objects make it easy to represent real-life entities such as students, cars, employees, books, bank accounts, and customers in software systems.

Applications include:

  • Designing banking systems
  • Creating games with characters and weapons
  • Managing school or college records
  • Building e-commerce systems with products and users
  • Developing graphical user interface elements like buttons and windows

Summary

  • A class is a blueprint.
  • An object is an instance of a class.
  • Classes define structure and behavior; objects use them in memory.
  • Important terms to remember: class, object, data member, member function, instance, blueprint