Deep Dive into Classes and Objects

Welcome, fellow programmers! Here we will dive into the world of classes and objects.

Don’t worry if you’re new to this concept — by the end of this post, you’ll be at class and object concepts.

First, let’s define what exactly a class is,

A class is an Abstract Data Type (ADT).

To break down this definition, let’s explore each word.

Firstly, consider the data type as a container for storing data. Like containers, data types have specific sizes, shapes, and limits to hold content. Each data type, such as int for integers, has its unique logical shape, size, and constraints.

Now, when we talk about a class as a data type, it raises the question of what shape and size a class possesses.

The answer lies in the term abstract.

Unlike specific shapes like squares, rectangles, or circles, an abstract shape doesn’t have a predefined form; it’s more flexible. Similarly, a class doesn’t come with a predetermined shape. Instead, when we create a class, we define its shape, which is why we refer to it as an abstract data type.

class Car{

   // properties
     String name;
     float price;

}

In this code snippet, a data type ( class ) named Car is defined. This class is designed to store two types of data simultaneously: a string for the Car’s name and a float for its price. It’s important to note that this class can be utilized for various data types, not just limited to strings and integers. Note that those data types inside the class are called properties.

The class goes beyond a typical data type; it is referred to as abstract, signifying enhanced flexibility. So, in a class, we not only create data types but also decide how they should behave by adding special functions. and we call these functions member functions.

class Car{
    // Properties
    String name;
    float price;

    // Behaviors
    public void run() {
        // Code for eating behavior
    }

    public void stop() {
        // Code for sleeping behavior
    }
}

In this code snippet, the Car class not only defines properties like run and stop but also incorporates behaviors through member functions. This is achieved by introducing functions within the class to represent actions or activities associated with a Car. In this example, two member functions, run and stop, are added to depict specific behaviors related to a Car.

Understanding Objects

When you look up the definition of a class on the internet, it often says it’s like a blueprint for creating objects. Think of it like a blueprint or map for a home. Initially, it’s just a structure on paper, not taking up any physical space. Only when we actually build a home based on that blueprint does it become valuable, and then it occupies real space.

Certainly! Consider this analogy: even if you’re aware of the shape of a container, it doesn’t occupy space until you actually have it. Similarly, creating a class doesn’t consume memory until you create a variable of it.

When you create a variable of a class, it takes up memory. This variable is what we call an object of the class or an instance of the class. The object now has a physical existence in the system’s memory. The process of creating an object of a class is termed instantiation.

Car myCar = new Car();

We have created a data type or template class for a Car. As you know we create as many variables as we want of any datatypes. similarly, we can create many objects of the Car class.

The new Car() statement will create an object on the HEAP region of the memory dynamically at the run time. The reference of the object will be assigned to the myCar reference variable of the class Car. The myCar variable is created on the STACK. At runtime, an object of a class is dynamically created on heap memory, which is referenced by a variable that resides on the stack

Reference and Object memory view


Recommended Free Ebook
Similar Articles