Lesson 0.4: How Objects Work Together
Useful programs rarely rely on one object. An OpMode coordinates a robot object, and that robot object can hold an arm object, an intake object, and a lift object.
This structure forms a has-a relationship. A robot has an arm. In Java, one object represents that relationship by storing a reference to another object in a field.
One Object Holds Another
public class PracticeRobot {
public RobotArm arm;
public PracticeRobot() {
arm = new RobotArm("main arm");
}
public void stopAll() {
arm.stop();
}
}
The PracticeRobot object and the RobotArm object are separate objects. The arm field stores the reference that connects them.
This arrangement is called composition. You only need the basic idea now. Unit 13 covers how to design safe class boundaries and decide what each object should expose.
Follow a Call Across Objects
public class ArmTeleOp extends OpMode {
private PracticeRobot robot;
@Override
public void init() {
robot = new PracticeRobot();
}
@Override
public void loop() {
robot.arm.move(-gamepad1.left_stick_y);
}
}
Two special parts of this example describe its relationship to the FTC SDK:
| Word | Meaning here |
|---|---|
extends | Declare that ArmTeleOp is a more specific kind of OpMode and receives its available members. |
@Override | Ask Java to check that this method replaces a method defined by OpMode. It is an annotation, not a keyword. |
When loop() reaches the final line:
- The OpMode reads its
robotfield. - Java follows that reference to the
PracticeRobotobject. - Java reads the robot's
armfield. - Java follows that reference to the
RobotArmobject. - Java runs
move(...)on that arm.
Each dot moves from one object to one of its fields or methods. Read a long expression from left to right and ask which object each part selects.
Class Definition Versus Running Order
Java does not run a whole class from top to bottom. A class provides definitions. Work happens when code creates an object, calls a constructor, or calls a method.
The FTC SDK creates the OpMode and calls its lifecycle methods. Your OpMode then creates or initializes the objects it needs and calls their methods.
Terms to Keep
| Term | Meaning |
|---|---|
| Has-a relationship | One object uses another object as a part. |
| Composition | Building an object from references to other objects. |
| Member | A field, constructor, or method declared by a class. |
| Dot notation | Syntax that selects a member from an object or class. |
| Call chain | The sequence of method calls from one object to another. |
Check Your Understanding
Does PracticeRobot become a RobotArm?
No. PracticeRobot has a RobotArm. The two objects have different classes and responsibilities.
How many objects does new PracticeRobot() create in this example?
It creates the PracticeRobot object, then its constructor creates one RobotArm object.
Which object receives the move call?
The RobotArm object stored in robot.arm receives it.
Where You Will Use This
Lessons 13.8 and 13.9 build RobotHardware.java and call its subsystem objects from a real TeleOp.
Ready to move on?
Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.
Stuck on this lesson?
Ask about anything on this page. It can see which lesson you have open and which part you are reading.