Classes and Object in Programming

Last Updated : 24 Mar, 2026

In Data Structures, programs often work with complex data and operations that need to be organized efficiently. Object-Oriented Programming concepts like classes and objects help structure data and related operations in a clear and manageable way.

  • Combines data (variables) and operations (functions) within a single structure.
  • Helps manage complex implementations by keeping related functionality together.
  • Improves reusability and organization when implementing data structures.

For Example, Dog is a class, Tommy is an object of that class.

Class_Object_example

Class

A class is a blueprint or template used to create objects. It defines the structure that specifies the data members (attributes) and functions (methods) an object will contain. A class typically contains two main components:

1. Attributes (Data Members): Attributes are the properties or characteristics of an object. They store information related to the object.

2. Methods (Functions): Methods define the actions or behaviors that an object can perform.

Features of a Class

  • Describes the data and operations related to a particular entity.
  • Serves as a reusable template from which multiple objects can be created.
  • Helps organize code by grouping related variables and functions.
  • Supports modular programming, making programs easier to understand and maintain.
C++
class Car
{
  public:
    // Attribute
    string color;
    string model;

    // Method
    void startEngine()
    {
        cout << "Engine started" << endl;
    }
};
Java
public class Car {
    
    // Attribute
    String color;
    String model;
    
    // Method
    public void startEngine()
    {
        System.out.println("Engine started");
    }
}
Python
class Car:

    # Attribute
    def __init__(self, color, model):
        self.color = color
        self.model = model

    # Method
    def start_engine(self):
        print("Engine started")
C#
class Car {

    // Attribute
    public string color;
    public string model;

    // Method
    public void StartEngine()
    {
        Console.WriteLine("Engine started");
    }
}
JavaScript
class Car {
    constructor(color, model) {
        //Attribute
        this.color = color;
        this.model = model;
    }
    
    //Method
    startEngine() {
        console.log("Engine started");
    }
}

Object

An object is an instance of a class. It represents a real-world entity created using the class blueprint

  • Stores actual values for class attributes.
  • Allows you to call methods defined in the class.
  • Multiple objects can exist from the same class, each holding different data.
C++
using namespace std;

// Define a class
class Car
{
  public:
    // Attribute
    string color;
    string model;

    // Method: starts the engine
    void startEngine()
    {
        cout << model << " engine started" << endl;
    }

    // Method: stops the engine
    void stopEngine()
    {
        cout << model << " engine stopped" << endl;
    }
};

int main()
{
    // Create first car object
    Car myCar;
    myCar.color = "Red";
    myCar.model = "Toyota";

    // Use attributes and methods
    cout << "My car color: " << myCar.color << endl;
    myCar.startEngine();
    myCar.stopEngine();

    return 0;
}
Java
// Define a class
class Car {
    // Attribute
    String color;
    String model;

    // Method: starts the engine
    void startEngine() {
        System.out.println(model + " engine started");
    }

    // Method: stops the engine
    void stopEngine() {
        System.out.println(model + " engine stopped");
    }
}

public class Main {

    public static void main(String[] args) {

        // Create first car object
        Car myCar = new Car();
        myCar.color = "Red";
        myCar.model = "Toyota";

        // Use attributes and methods
        System.out.println("My car color: " + myCar.color);
        myCar.startEngine();
        myCar.stopEngine();
    }
}
Python
# Define a class
class Car:

    # Method: constructor (optional)
    def __init__(self):
        self.color = ""
        self.model = ""

    # Method: starts the engine
    def startEngine(self):
        print(f"{self.model} engine started")

    # Method: stops the engine
    def stopEngine(self):
        print(f"{self.model} engine stopped")

if __name__ == "__main__":

    # Create first car object
    myCar = Car()
    myCar.color = "Red"
    myCar.model = "Toyota"

    # Use attributes and methods
    print("My car color:", myCar.color)
    myCar.startEngine()
    myCar.stopEngine()
C#
using System;

// Define a class
class Car {
    // Attribute
    public string color;
    public string model;

    // Method: starts the engine
    public void startEngine() {
        Console.WriteLine(model + " engine started");
    }

    // Method: stops the engine
    public void stopEngine() {
        Console.WriteLine(model + " engine stopped");
    }
}

class main {

    // driver code
    static void Main() {

        // Create first car object
        Car myCar = new Car();
        myCar.color = "Red";
        myCar.model = "Toyota";

        // Use attributes and methods
        Console.WriteLine("My car color: " + myCar.color);
        myCar.startEngine();
        myCar.stopEngine();
    }
}
JavaScript
// Define a class
class Car {

    // Method: constructor
    constructor() {
        this.color = "";
        this.model = "";
    }

    // Method: starts the engine
    startEngine() {
        console.log(this.model + " engine started");
    }

    // Method: stops the engine
    stopEngine() {
        console.log(this.model + " engine stopped");
    }
}

// driver code
const myCar = new Car();
myCar.color = "Red";
myCar.model = "Toyota";

// Use attributes and methods
console.log("My car color:", myCar.color);
myCar.startEngine();
myCar.stopEngine();

Output
My car color: Red
Toyota engine started
Toyota engine stopped

Classes and Objects in Data Structures

In Data Structures, classes and objects help organize data and the operations performed on that data. A class acts as a blueprint that defines the structure and behavior, while an object is an instance of that class used to perform operations.

  • Allow you to reuse code (write once, use many times).
  • Make it easier to represent real-world structures like: Stack (push, pop), Queue (enqueue, dequeue) and Linked List (nodes with data + next pointer).

Example: Stack Using Class and Object

In a Stack data structure, elements are inserted and removed following the LIFO (Last In, First Out) principle. A class can be used to define the structure of the stack and the operations performed on it.

Here, the class represents the stack design, while the objects allow us to perform stack operations such as push and pop.

C++
using namespace std;

// Class representing a Stack
class Stack {
  public:
    int arr[5];
    int top = -1;

    // Method to push element
    void push(int x) {
        if(top == 4) {
            cout << "Stack Overflow\n";
            return;
        }
        arr[++top] = x;
    }

    // Method to pop element
    void pop() {
        if(top == -1) {
            cout << "Stack Underflow\n";
            return;
        }
        top--;
    }

    // Method to display stack
    void display() {
        for(int i = top; i >= 0; i--) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
};

int main() {

    // Creating stack object
    Stack s;

    // Performing operations
    s.push(10);
    s.push(20);
    s.push(30);

    cout << "Stack elements: ";
    s.display();

    s.pop();

    cout << "After pop: ";
    s.display();

    return 0;
}
Java
// Class representing a Stack
class Stack {
    int[] arr = new int[5];
    int top = -1;

    // Method to push element
    void push(int x) {
        if(top == 4) {
            System.out.println("Stack Overflow");
            return;
        }
        arr[++top] = x;
    }

    // Method to pop element
    void pop() {
        if(top == -1) {
            System.out.println("Stack Underflow");
            return;
        }
        top--;
    }

    // Method to display stack
    void display() {
        for(int i = top; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}

public class Main {

    // driver code
    public static void main(String[] args) {

        // Creating stack object
        Stack s = new Stack();

        // Performing operations
        s.push(10);
        s.push(20);
        s.push(30);

        System.out.print("Stack elements: ");
        s.display();

        s.pop();

        System.out.print("After pop: ");
        s.display();
    }
}
Python
# Class representing a Stack
class Stack:

    def __init__(self):
        self.arr = [0]*5
        self.top = -1

    # Method to push element
    def push(self, x):
        if self.top == 4:
            print("Stack Overflow")
            return
        self.top += 1
        self.arr[self.top] = x

    # Method to pop element
    def pop(self):
        if self.top == -1:
            print("Stack Underflow")
            return
        self.top -= 1

    # Method to display stack
    def display(self):
        for i in range(self.top, -1, -1):
            print(self.arr[i], end=" ")
        print()

# driver code
if __name__ == "__main__":

    # Creating stack object
    s = Stack()

    # Performing operations
    s.push(10)
    s.push(20)
    s.push(30)

    print("Stack elements: ", end="")
    s.display()

    s.pop()

    print("After pop: ", end="")
    s.display()
C#
using System;

// Class representing a Stack
class Stack {
    public int[] arr = new int[5];
    public int top = -1;

    // Method to push element
    public void push(int x) {
        if(top == 4) {
            Console.WriteLine("Stack Overflow");
            return;
        }
        arr[++top] = x;
    }

    // Method to pop element
    public void pop() {
        if(top == -1) {
            Console.WriteLine("Stack Underflow");
            return;
        }
        top--;
    }

    // Method to display stack
    public void display() {
        for(int i = top; i >= 0; i--) {
            Console.Write(arr[i] + " ");
        }
        Console.WriteLine();
    }
}

class main {

    // driver code
    static void Main() {

        // Creating stack object
        Stack s = new Stack();

        // Performing operations
        s.push(10);
        s.push(20);
        s.push(30);

        Console.Write("Stack elements: ");
        s.display();

        s.pop();

        Console.Write("After pop: ");
        s.display();
    }
}
JavaScript
// Class representing a Stack
class Stack {
    constructor() {
        this.arr = new Array(5);
        this.top = -1;
    }

    // Method to push element
    push(x) {
        if(this.top === 4) {
            console.log("Stack Overflow");
            return;
        }
        this.arr[++this.top] = x;
    }

    // Method to pop element
    pop() {
        if(this.top === -1) {
            console.log("Stack Underflow");
            return;
        }
        this.top--;
    }

    // Method to display stack
    display() {
        for(let i = this.top; i >= 0; i--) {
            process.stdout.write(this.arr[i] + " ");
        }
        console.log();
    }
}

// driver code
const s = new Stack();

// Performing operations
s.push(10);
s.push(20);
s.push(30);

process.stdout.write("Stack elements: ");
s.display();

s.pop();

process.stdout.write("After pop: ");
s.display();

Output
Stack elements: 30 20 10 
After pop: 20 10 
Comment