Method and Constructor Overloading

intermediate80 min

Overloading

Overloading means two or more methods (or constructors) share a name but differ in parameter types or counts. The compiler picks the match at compile time.

Use it when the same action needs optional arguments—for example, moveForward() and moveForward(double speed) instead of unrelated method names.

Constructor overloading

Several constructors can initialize the same class with different levels of detail.

Basic constructor overloading

Constructor Chaining with this()

Shared setup can call another constructor with this(...) as the first line in the constructor.

Constructor chaining

Constructor Overloading Rules:

Important Guidelines:

  • Different Parameters: Each constructor must have different parameter types or numbers
  • this() First: Constructor chaining with this() must be the first line
  • No Return Type: Constructors never have return types (not even void)
  • Same Name: All constructors have the same name as the class
  • Unique Signatures: Cannot have two constructors with identical parameter lists

Method Overloading

Just like constructors, methods can be overloaded to provide different ways of performing the same operation. This is extremely useful in robotics for creating flexible movement, sensor reading, and control methods.

Basic method overloading

Method Chaining

Similar to constructors, overloaded methods can call each other to reduce code duplication. The simpler methods can call the more complex ones with default values.

Method overloading with chaining

Practical FRC Examples

Let’s look at some real-world FRC robotics examples where overloading makes the code much more flexible and easier to use in competition scenarios.

FRC drivetrain example

Try It Yourself:

Exercise 1: Overloading Practice

  • Create a Servo class with multiple constructors for different initialization options
  • Build a Sensor class with overloaded reading methods for different sampling options
  • Design an Autonomous class with overloaded movement commands
  • Create a Competition class with multiple scoring and timing methods
Practice creating overloaded constructors and methods for FRC robotics scenarios: