Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Harnessing the Power of Objects Overload Methods

I'm stuck.

Hi, can somebody please give me the answer to "Overload Methods"? I'm trying to complete the second task but I'M STUCK.

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;
  private int lapsDriven;

  public GoKart(String color) {
    this.color = color;
  }

  public String getColor() {
    return color;
  }
  public void charge() {
    barCount = MAX_BARS;
  }

  public boolean isBatteryEmpty() {
    return barCount == 0;
  }

  public boolean isFullyCharged() {
    return MAX_BARS == barCount;
  }

  public void drive(int lapsAmount) {
    lapsDriven++;
    barCount--;
    lapsDriven += lapsAmount;
    barCount -= lapsAmount;
  }
 public void drive1(); 

}
Jenna Dercole
Jenna Dercole
30,224 Points

Hi Andres,

When overriding methods, the first thing you want to check for is that both methods' names match. Your last method should be named "public void drive() {}" -- you can remove the 1. The question is asking you to override the first drive() method, this time without parameters. When you define the second method, nothing should be inside the parentheses, which is what you have--good. Then, in the body of the second method, call your first drive() method passing in '1' as a parameter there.

Try that and let me know if that fixes your error.

3 Answers

Oleksiy Slisenko
Oleksiy Slisenko
7,002 Points

Dear Andrés !

When You overload method You have to use the same name of a function, but with different parameter list, e.g.:

Original method:

public void drive() {
    lapsDriven++;
    barCount--;
}

and, overloaded method:

public void drive(int lapsAmount) {
    lapsDriven += lapsAmount;
    barCount -= lapsAmount;
}

Notice, that the function name drive is the same in both implementations, but the code and parameter list are defferent. Good luck !

Oleksiy Slisenko
Oleksiy Slisenko
7,002 Points

P.S. Sorry ! I wrote functions, but had in mind methods !!! :-)

And, of course, both methods got to be implemented under the same object.

Hi jenna, thanks for the answer. So it should be public void(), but I dont know what goes inside the code block to call the first method.

Rafael Miranda
Rafael Miranda
17,121 Points

Hi Andrés,

if you want to call the first method in the second just call it like:

public void drive1(){
   drive();
}