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)
What is a Class?
Section titled “What is a Class?”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.
What is an Object?
Section titled “What is an Object?”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.
Key intuition:
Section titled “Key intuition:”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.
Note: What is UML?
Section titled “Note: What is UML?”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.
Classes and Objects in UML and Java
Section titled “Classes and Objects in UML and Java”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. |
Example
Section titled “Example”UML
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.
Creating an Object
Section titled “Creating an Object”A class only defines what an object should look like. To use it, you must instantiate (create) an object.
Car myCar = new Car();Here:
Caris the class.myCaris the object.new Car()creates a new instance of theCarclass.
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."Summary
Section titled “Summary”- 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.