the video version
The same lesson as a narrated video. Watch it on YouTube
Real robot systems are things with both data and behavior: a drivetrain has motors (data) and can drive (behavior). Object-oriented programming bundles related data and the methods that act on it into one unit. WPILib is built this way top to bottom, so this is the lesson that makes the rest of the library make sense.
Classes vs. Objects#
A class is a blueprint; an object is a real thing built from it. One class can produce many objects.
class Elevator {
double heightMeters = 0.0; // field (data)
boolean atTop = false; // field
void raise(double amount) { // method (behavior)
heightMeters += amount;
}
}
- Fields are the object's data (its state).
- Methods are what it can do.
You build one with new, then use the dot operator to reach its fields and methods:
Elevator elevator = new Elevator();
elevator.raise(0.2);
A special method called a constructor runs at new and sets up the object's starting state.
Encapsulation: Keep Internals Private#
Good classes hide their data behind a clean set of methods. Marking a field private stops outside code from poking at it directly; public methods control access. That's why WPILib hands you motor.set(0.5) instead of exposing raw PWM signals — the internals can change without breaking your code.
Inheritance: Building on What Exists#
Inheritance lets a new class reuse and extend an existing one with extends. The original is the superclass (parent); the new one is the subclass (child).
class Robot extends TimedRobot {
@Override
public void teleopPeriodic() {
// your code, called automatically every cycle
}
}
The rules worth remembering:
- A subclass inherits the superclass's fields and methods.
- Java is single inheritance — one direct superclass.
- Constructors aren't inherited, but a subclass calls the parent's with
super(...). - A subclass can override an inherited method;
@Overridetells the compiler you meant to.
This is why nearly every FRC project opens with class Robot extends TimedRobot. TimedRobot already handles the hard parts — talking to the Driver Station, running the loop at 50 Hz, switching between autonomous and teleop. You inherit all of it and override robotInit, teleopPeriodic, and friends to fill in your robot.
Interfaces#
An interface is a contract: a list of methods a class promises to provide. WPILib's Subsystem is one. A class implements an interface, and unlike with extends it can implement many. For now, just read implements as "this class agrees to provide these methods."
Why This Unlocks WPILib#
When you see extends, new, a dot-call, or @Override in WPILib code, you now know what's happening: you're standing on classes the library authors wrote and customizing them through inheritance. That's the jump from copying examples to writing your own robot code.
the part worth keeping
Key takeaways
- A class is a blueprint; an object is a concrete instance created with `new`.
- Fields hold an object's data; methods define its behavior; the dot operator accesses both.
- Encapsulation (`private` fields, `public` methods) hides internals and is why WPILib exposes clean APIs.
- Inheritance via `extends` reuses a superclass; Java allows only single inheritance and everything descends from `Object`.
- FRC robots subclass `TimedRobot` and `@Override` its methods, inheriting the 50 Hz loop and driver-station handling for free.
Programming, Controls & SensorsProgramming Foundations: A Java Primerlesson 3 of 4
Keep going
Take the quiz+10 XP with an accountMore in Programming Foundations: A Java Primer
where this came from
Sources and corrections
This lesson is AI-assisted: drafted from primary sources, then reviewed and edited by hand. Errors still get through. When one is reported we fix it and write down what changed, in public, in the corrections log.
sources and further reading
- docs.oracle.comOracle Java Tutorials: What Is Inheritance?
- docs.oracle.comOracle Java Tutorials: Inheritance (Subclasses and Superclasses)
- docs.oracle.comOracle Java Tutorials: Object-Oriented Programming Concepts
- docs.wpilib.orgWPILib Docs: Creating a Robot Program
clipped to this lesson
Articles that go further on this
The lesson gets you through the topic. These go wider on it, and they read in one sitting.
- 16 min readFRC Elevator and Arm Design: Staging, Rigging, Motors, Gravity, and SafetyA primary-source FRC guide to designing elevators and arms: cascade vs continuous rigging, staging, motor and gear-ratio sizing, gravity math, and safe holding./blogread it
- 20 min readBuilding an FRC Scouting App: Data Model, TBA/Statbotics APIs, and PicklistsBuild an FRC scouting app: match and pit scouting, a simple data model, pulling schedules and EPA from the TBA and Statbotics APIs, and building a picklist./blogread it
- 7 min readFRC Programming Languages: Java vs C++ vs PythonAn honest, beginner-friendly comparison of the three WPILib-supported FRC languages — Java, C++, and Python (RobotPy) — with vendor support and how to choose./blogread it
answer sheet
Lesson quiz
All 4 right completes the lesson. Miss one and only that question comes back, anything you already answered correctly stays banked.
0 of 4 answered
01What is the relationship between a class and an object?
02In `class Robot extends TimedRobot`, what is TimedRobot?
03How many direct superclasses can a Java class have?
04What does encapsulation do in a well-designed class?
Answer every question to submit.
All 51 lessons in Programming, Controls & Sensorsopenclose
01 / prerequisites
02 / foundations-tools-and-first-program
03 / robot-program-and-command-based
04 / motors-and-control
05 / autonomous-trajectories-simulation
06 / sensing-fundamentals
07 / encoders
08 / gyros-imus-orientation
09 / closed-loop-control
10 / vision-pose-estimation
11 / worked-examples-mini-projects
- Not read yet:Mini-Project: A Closed-Loop Elevator with Motion Magic
- Not read yet:Mini-Project: A Velocity-Controlled Shooter on REVLib
- Not read yet:Mini-Project: A Teleop Swerve Drive Subsystem
- Not read yet:Mini-Project: An Autonomous Routine with PathPlanner
- Not read yet:Mini-Project: Vision-Aligned Scoring with Limelight
12 / common-mistakes-troubleshooting
13 / advanced-techniques-case-studies
- Not read yet:State-Space Control and Kalman Filtering
- Not read yet:Log Replay Architecture with AdvantageKit
- Not read yet:Advanced Pose Estimation: Multi-Tag Fusion and Standard Deviations
- Not read yet:Robot Coordination, Alerts, and Operator Feedback
- Not read yet:Case Study: Hardening Software Before an Event