Class & Object

Lesson 0003 — 20 min read

Class = blueprint. Object = instance built from that blueprint, with its own memory and state.

What is a Class?

A class is a template that defines attributes (data) and methods (behavior) common to all objects of its type. A class does not occupy memory on its own — it's just a definition.

class Employee {
    private int salary;
    public String employeeName;

    public void setName(String s) {
        employeeName = s;
    }

    public void setSalary(int val) {
        salary = val;
    }

    public int getSalary() {
        return salary;
    }
}
class Employee {
private:
    int salary;
public:
    string employeeName;

    void setName(string s) { employeeName = s; }
    void setSalary(int val) { salary = val; }
    int getSalary() { return salary; }
};
class Employee:
    def __init__(self):
        self.__salary = 0
        self.employeeName = ""

    def setName(self, name):
        self.employeeName = name

    def setSalary(self, val):
        self.__salary = val

    def getSalary(self):
        return self.__salary

Key idea: the class is just the blueprint. No memory is allocated until objects are created.

What is an Object?

An object is an instance of a class. When created, memory is allocated and it holds its own copy of the attributes. Each object is independent — obj1's state doesn't affect obj2's.

Employee obj1 = new Employee();
obj1.setName("Raj");
obj1.setSalary(10000);

Employee obj2 = new Employee();
obj2.setName("Rahul");
obj2.setSalary(15000);

System.out.println("Salary of " + obj1.employeeName + " is " + obj1.getSalary());
System.out.println("Salary of " + obj2.employeeName + " is " + obj2.getSalary());
Employee obj1;             // stack
obj1.setName("Raj");
obj1.setSalary(10000);

Employee obj2;             // stack
obj2.setName("Rahul");
obj2.setSalary(15000);

cout << "Salary of " << obj1.employeeName << " is " << obj1.getSalary() << endl;
cout << "Salary of " << obj2.employeeName << " is " << obj2.getSalary() << endl;
obj1 = Employee()
obj1.setName("Raj")
obj1.setSalary(10000)

obj2 = Employee()
obj2.setName("Rahul")
obj2.setSalary(15000)

print("Salary of " + obj1.employeeName + " is " + str(obj1.getSalary()))
print("Salary of " + obj2.employeeName + " is " + str(obj2.getSalary()))

Each object has its own employeeName and salary in memory. Changing obj1 does nothing to obj2.

Attributes & Behaviors

Object Creation & Deletion

Language Creation Destruction
Java new Employee() — heap GC auto-collects when unreachable
C++ Employee e; — stack
new Employee() — heap
Stack: auto at scope end
Heap: manual delete
Python Employee() — heap (always) GC via ref counting + cycle detector

Stack vs Heap

Code trace: What does this print?

Employee e1 = new Employee();
e1.setName("Raj");

Employee e2 = new Employee();
e2.setName("Raj");

System.out.println(e1 == e2);
System.out.println(e1.employeeName.equals(e2.employeeName));

In C++, where is the object allocated?

Employee e;

Notes

Class vs Object :-

Class ⇒ blueprint ; no memory allocated until object creation

Object ⇒ instance of class ; gets its own memory and state

(eg Employee class ⇒ Raj's obj, Rahul's obj)

Attributes & Behaviors :-

a) Attributes ⇒ data / state (eg employeeName, salary)

b) Behaviors ⇒ methods / actions (eg setName(), getSalary())

Memory Management :-

Creation:

a) Java ⇒ new Employee() on heap ; reference on stack

b) C++ ⇒ Employee e on stack ; new Employee() on heap

c) Python ⇒ always heap ; variable is a reference

Deletion:

a) Java ⇒ GC auto-collects when unreachable (eg obj = null)

b) C++ ⇒ Stack: auto ; Heap: manual delete (leak risk)

c) Python ⇒ reference counting + GC ; del removes ref

Stack vs Heap :-

Stack ⇒ primitives + references ; auto-cleared on scope exit ; fast

Heap ⇒ objects ; managed by GC or manual memory ; flexible

Primary source: Oracle Java Tutorials — OOP Concepts. Ask me anything that's unclear.

← Prev: OOP Fundamentals Next: Attributes & Methods →

Questions? Ask your agent — you can follow up on any concept, quiz answer, or how this applies to a specific coding problem. Review the Java glossary or syntax quick reference for a compressed overview.