
Simplifying OOP in Java: Classes vs Objects vs Instances
A simple introduction to one of the most important starting points in Object-Oriented Programming: understanding the difference between classes, objects, and instances in Java.
Day 1: Classes vs Objects vs Instances
Everything in OOP starts here.
Before going deeper into concepts like encapsulation, inheritance, or polymorphism, it is important to clearly understand the difference between a class, an object, and an instance.
Class
A class is like a blueprint.
It defines what something is and describes the structure that objects created from it will follow.
In Java, a class can contain fields and methods that describe the data and behavior of that type.
Example:
class Car {}
Car myCar = new Car();Here, Car is the class.
Object
An object is the actual thing created from that class.
When we create an object, we are creating a real entity based on the blueprint.
Example:
new Car()
This creates an object from the Car class.
Instance
An instance is simply an object created from a class.
So if we create an object from the Car class, that object is also called an instance of Car.
Reference Variable
In Java, we often store that object in a variable.
Example:
Car myCar = new Car();
In this example:
Caris the classnew Car()creates the objectnew Car()is also an instance ofCarmyCaris the reference variable pointing to that object