Sitemap

Introduction to OOP-Class

4 min readMar 8, 2024

--

From this chapter, we will start our journey of OOP(Object Oriented Programming) by learning about the class and object.

Previous Chapter
First Chapter

Introduction to Class

Lets say, we want to develop an application that will represent a toy store. Each toy is identified by its ID. For each toy, we also need to know the price and the quantity.

Now how do we represent toys in our application? To answer the question first let us understand this:

We represented integers using the int data type, characters using the char data type, etc. like the following;

int myInt= 5;
char myChar = 'A';

For the variable myInt of type int, we have assigned a value of 5 and for the variable b of type char, we have assigned a value of ‘A’.

As we represented an integer with a variable and a primitive data type, similarly, we need a data type and a variable to represent the toy in our code. Now the question is how do we do that?

There are two things: First, we need to define a data type that can represent toy. With the following code, we can do so:

Fig 1.1: Toy Data Type

We use the public and class keyword to define a data type that will represent a toy in our store.

Now, the second question is how do we create a variable from the data type Toy we have just defined? The answer is similar to the primitive data type. Following is the code:

Toy car;

car is a variable of type Toy. The variable created from a class (Toy is a class here) is called a reference variable. You will see soon why it is named that way. Now, the car variable is our reference variable, keep it in mind.

Get rakibul rushel’s stories in your inbox

Join Medium for free to get updates from this writer.

But, we are missing something, don't we? A variable needs a value to be assigned otherwise what is the use of the variable? That is why we are going to assign a value to the variable car with the following code:

car = new Toy(); 

When a program runs JVM (Java Virtual Machine) allocates a memory area called HEAP from RAM.

Fig: 1.1

After the execution of the statement:

new Toy();

A portion of memory is taken from the HEAP (the large white rectangle) which contains ID (initialized with default value 0), price (initialized with default value 0), and quantity (initialized with default value 0). The large rectangle is the instance of the class Toy represented in HEAP at some memory location and this is done by the statement above. So, to create a instance you have to follow the following format:

new class_name();

new is the keyword in java to create an instance.

Now, how do we get hold of the instance we have just created, that is where we used reference variable:

car = new Toy();

Now, if you have a look at the Fig 1.1 the car variable referencing the instance. So, if you want to work with the instance we have to use the car reference variable.

Do we want a car toy in our store which costs 0 dollar? of course not. Lets update ID, price and quantity from 0 to some legitimate values. Following is the code:

car.ID = 10;
car.price = 30;
car.quantity = 50;

After the execution of the the above three statement the Fig 1.1 becomes:

Fig: 1.2

We used the java dot (.) operator to access a member variable of an instance. A member variable is a variable that is defined inside the class but outside any method or block. As methods and blocks are yet to be discussed, it is enough to know that the ID, price, and quantity are member variables for now. We assign price with the help of the following code using the dot operator:

car.price = 30;

An instance of a class is also called object. The type of instance or object is the class you defined.

You can change the member variables multiple times as you like;

car.price = 30;
car.price = 500;

That is it for this chapter.

Next Chapter

--

--