Sitemap
Nethmi Warnakula

Nethmi Warnakula

Difference between class and object in java

2 min readNov 21, 2022

--

Class is a blueprint or template which defines properties and behavior for creating objects.

Object is an instance of a class. We can create many objects using the same class template.

Class is like a cookie cutter and object is like a cookie. We can create many cookies using the same cutter.

Press enter or click to view image in full size

Example:

class Vehicle 
{
//----------attributes--------------
String model;
String color;
float price;
int seats;

//----------methods-----------------
public String start(){
System.out.println("Vehicle is starting");
}
public String park(){
System.out.println("Vehicle is parking");
}
}

Vehicle class have different attributes like model, class, price and seats. Also it has methods like start and park.

Get Nethmi Warnakula’s stories in your inbox

Join Medium for free to get updates from this writer.

We can create different objects using Vehicle class.

Vehicle car = new Vehicle();
car.model = "Alto";
car.color = "Red";

Vehicle van = new Vehicle();
van.model = "Toyota";
van.color = "White";
van.seat = 8;

Vehicle bicycle = new Vehicle();
bicycle.color = "Blue";
bicycle.model = "Bajaj";

Here car, van and bicycle are 3 objects that we have created using Vehicle class. We can create any number of objects using a class.

Key differences between Class and Object

Press enter or click to view image in full size

--

--