Summer · Week 5 of 8
OOP — Classes & Objects
The most important week in this course. Everything in FRC robot code is a class.
Why this matters: Every subsystem on 2974's robot is a class. Drivetrain.java, Shooter.java, Coral.java, Finger.java — all classes. Understanding this week means you can read and write real robot code.
Classes vs Objects
A class is a blueprint. An object is one specific thing built from that blueprint. You write the class once, then create as many objects as you need — each with their own independent data.
java — class definition
public class Motor { // Instance variables — each Motor object gets its own copy private int id; private double speed; private boolean isInverted; // Constructor — runs when you do "new Motor(...)" public Motor(int id, boolean isInverted) { this.id = id; this.isInverted = isInverted; this.speed = 0.0; } public void setSpeed(double speed) { this.speed = isInverted ? -speed : speed; } public double getSpeed() { return speed; } }
java — creating and using objects
Motor leftMotor = new Motor(1, false); Motor rightMotor = new Motor(2, true); // inverted leftMotor.setSpeed(0.5); rightMotor.setSpeed(0.5); System.out.println(leftMotor.getSpeed()); // 0.5 System.out.println(rightMotor.getSpeed()); // -0.5 — inverted!
Instance Variables
Data each object stores
Each object gets its own copy.
leftMotor.id and rightMotor.id are completely separate.Constructor
Runs on
newSame name as class. No return type. Sets the initial state of the object.
this
Refers to current object
When a parameter and instance variable share a name,
this.speed means the instance variable.Encapsulation
Private + getters/setters
Keep data private, expose it through methods that can validate input. Prevents accidental corruption.
Access Modifiers
| Modifier | Who can access it | When to use |
|---|---|---|
| public | Anyone, anywhere | Methods you want others to call |
| private | Only inside this class | Instance variables (almost always) |
| protected | This class + subclasses | When you plan for inheritance |
Live Class Builder
Type field declarations and watch the class generate
Fill in the Blanks
// Declare a private double field
double motorSpeed;
double motorSpeed;
// Constructor for class Intake taking int motorID
public (int motorID) { }
public (int motorID) { }
// Create a new Motor with id=3, inverted=false
Motor m =
Motor m =
Knowledge Check
Coding Challenge
Build an Intake Class
Write a real FRC subsystem class from scratch
Write an Intake class with private fields int motorID, double speed, boolean isRunning. Constructor takes motorID, sets speed=0 and isRunning=false. Methods: start(double speed), stop(), isRunning(), getSpeed(). Then create two objects and test them.