What Is An Object?

Balamurugan M
3 min readMar 21, 2024

--

Object is an Instance of the class. Means once we created a class bike the blueprint of bike is ready. It can never be called a bike. So we need to create a real object with the help of blueprint/template that is class.
An object can be created with the help of the keyword new, that creates an object with all of the behaviours of the blueprint.
We can access the behaviour and state by creating objects. The Dot Operator(.) used to access the instances variables and methods inside the class.
Object lives in heap. when a new keyword followed by the constructor of the class object is created in the heap memory.
We can access the created object using the reference variable. Class must be the datatype of the object reference variable.
We can also store the object in the reference variable of type object.
There is a disadvantage in using object as datatype because you can only perform the access what an object does. we cannot access the methods of the object which is stored inside an object.
Let’s be clear on this, Robot is a class, we created an robot as reference variable of type Robot to access all the robot state and behaviour.It doesn’t create any problem but when we create a object using reference variable type object we cannot able to access the robot state and behaviour instead we can access to only the methods that common for all objects.
getClass(),getHashCode(),toString(),equals(),clone() .
We bring back the behaviour by casting it to the actual object. ex- robot ref variable holds the object inside object it has Robot object. when we create a ref variable of type Robot with casting Robot type with the object.
We can also restrict the other class to create object if needed by marking the constructor of the class private.
when an object has no reference variable associate with it. The garbage collector wipes it out like a trash and always makes the memory space clean in heap.

Fig 1.4 Object and Class Relationship

How It Is Different From Class?

Without a class, an object cannot be created. Without an object class is just a template waiting for making an object.
Blueprint for creating a mobile is class and object is a new mobile phone created by using the blueprint.
We can create as many mobile phones object with the help of the mobile phone class.
Class is like a human body without soul. soul is the object which stores information and makes us to-do work.
Class does not contain any values for the instance variables because it is a template but each object have its own unique value of the class in it.

Fig 1.5 Object in Heap

--

--

Balamurugan M
Balamurugan M

No responses yet