codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Classes, Objects, & Instantiation

Updated 2026-05-06
2 min read

Classes, Objects, & Instantiation

The most fundamental concept in OOP is the distinction between a Class and an Object. Understanding this relationship is the key to mastering every other OOP principle.

1. What is a Class?

A Class is a user-defined blueprint or prototype from which objects are created. It defines a set of attributes (variables) and methods (functions) that the created objects will have.

Think of a class as an architectural blueprint for a house. The blueprint itself is not a house—you cannot live in a blueprint. It simply defines the structure: how many rooms, where the doors go, and where the plumbing runs.

// The Blueprint (Class)
class Car {
    // Attributes (State)
    String brand;
    String color;
    int speed;

    // Methods (Behavior)
    void accelerate() {
        this.speed += 10;
    }

    void brake() {
        this.speed -= 10;
    }
}

2. What is an Object?

An Object is a specific instance of a class. It is a concrete entity that exists in memory, holds actual values for the attributes defined in the class, and can execute the methods.

If the Car class is the blueprint, then a specific red Toyota with a speed of 60 km/h is an Object (an instance) of that class.

3. Instantiation

The process of creating an object from a class is called Instantiation. In most OOP languages, this is done using the new keyword.

// Instantiation: Creating actual Objects from the Class
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.color = "Red";

Car yourCar = new Car();
yourCar.brand = "BMW";
yourCar.color = "Black";

When new Car() is executed:

  1. The JVM allocates a block of memory on the Heap large enough to store all the attributes defined in the Car class.
  2. The attributes are initialized to their default values (null for Strings, 0 for integers).
  3. The Constructor method is called (if defined) to perform custom initialization.
  4. A reference (memory address) to this newly created memory block is returned and stored in the variable myCar.

4. The this Keyword

Inside a method, how does the object know which object's data it should operate on? When you call myCar.accelerate(), the Java runtime implicitly passes a hidden reference to myCar into the accelerate() method. This hidden reference is accessed via the this keyword.

void accelerate() {
    this.speed += 10; // 'this' refers to whichever object called the method
}

5. Memory Layout

It is crucial to understand the difference between the reference variable and the actual object:

  • Stack: The reference variable myCar (which is just a memory address pointer) is stored on the Stack.
  • Heap: The actual object data (brand, color, speed) is allocated on the Heap.

When you write Car anotherRef = myCar;, you are NOT creating a new car. You are copying the memory address. Both anotherRef and myCar now point to the exact same object on the Heap. Modifying the car through one reference will be visible through the other.



PreviousProcedural vs Object-OrientedNextConstructors & Destructors

Recommended Gear

Procedural vs Object-OrientedConstructors & Destructors