Skip to content

What are Objects?

To understand what an “object” is, it is first essential to understand what a “class” is.

(And no, they aren’t Tank, Fighter, Assassin, Marksman, Mage, and Support)

In OOP, a class is like a blueprint from which individual objects are created. It describes the attributes (data) and methods (behaviors) that objects will have, but it is not an object itself. Objects are the actual instances created from the class.

An object is an instance of a class.

If a class is the blueprint, an object is the actual thing created from that blueprint.

Objects have their own attributes and can perform methods defined by their class. You can create many objects from the same class, and each object can have different values for its attributes.

Think of a class as the blueprint for a house. The blueprint describes how the house should be built, but it is not the house itself. Once the blueprint is used to build a house, that house becomes an object. Many houses can be built from the same blueprint, and each one can have different colors, sizes, or owners.

For the rest of this reviewer and course, you will be dealing with UML (Unified Modeling Language), a standard way of drawing diagrams that show how a program is designed before it is built.

It helps programmers visualize classes, objects, and the relationships between them, making it easier to plan, understand, and communicate the architecture of a software system.

The table below helps you imagine how the same class could be represented in UML and Java:

UML Java
The class name appears at the top of the UML box. The class name is declared using the class keyword.
Attributes are listed in the middle section. Attributes are declared as variables inside the class.
Methods are listed in the bottom section. Methods are written as functions inside the class.

UML

UML Example 1

Equivalent Java Code

class Car {
String brand;
int year;
void start() {
System.out.println("Car started.");
}
void stop() {
System.out.println("Car stopped.");
}
}

If you don’t know what the ‘-’ and ‘+’ signs mean and what void means, don’t worry! We’ll learn more in the next chapter.

A class only defines what an object should look like. To use it, you must instantiate (create) an object.

Car myCar = new Car();

Here:

  • Car is the class.
  • myCar is the object.
  • new Car() creates a new instance of the Car class.

Once an object has been created, you can access its attributes and methods using the dot (.) operator.

myCar.brand = "Toyota";
myCar.year = 2024;
myCar.start(); // This one prints out "Car started."
  • A class is a blueprint or template.
  • An object is an instance created from a class.
  • Attributes store an object’s data.
  • Methods define an object’s behavior.
  • UML provides a visual representation of classes before they are implemented in Java.