Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Harnessing the Power of Objects Overload Methods

why a1.

Why do i need to call a1, at the second drive method. Furthermore i have a hard time understanding the concept of having two drive-methods, i understand that one takes parameters, and the other doesn't but how is thi usefull, and how can you call them separately if they are called the same?

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() {
    lapsDriven++;
    barCount--;
  }
}

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,076 Points

I recommend you re-watch the video on overloading methods to gain a better understanding of them. However, let me shed some light into this mystery for you.


Overloaded methods can be extremely important for how you architect your application. Imagine a scenario where you need to have a method that can accept multiple different types. Instead of having to account for each different variable type and then check in the method if that parameter is null, in order to determine what was inputted by the user. We can use method overloading to accomplish the same task much easier...here let me do an example for you:

Imagine we are tasked with writing a double findArea(); that finds the area for different shapes, let's pretend we have three defined shapes: Circle, Square, and Triangle.

We could implement the method like so:

public double findArea(Circle c, Square s, Triangle t) {
        if(s == null && t == null) { //Find area of circle
                 return Math.Pi * Math.pow(c.radius, 2);
        }
        else if(c == null && t == null) { //Find area of square
                 return Math.pow(s.side, 2);
        }
        else if(c == null && s == null) { //Find area of triangle
                 return (t.height * t.base)/2;
        }
       // Throw some exception
}

// Then we can call it like so
Triangle t1 = new Triangle(1, 2, 3);
findArea(null, null, t1); //Get the area of a triangle

Lot's of work right? Well we can instead use overloading to do the following:

public double findArea(Circle c) {
     return Math.Pi * Math.pow(c.radius, 2);
}

public double findArea(Square s) {
     return Math.pow(s.side, 2);
} 

public double findArea(Triangle t) {
     return (t.height * t.base)/2;
} 

// You can call it like so
Square s1 = new Square(5);
Circle c1 = new Circle(3);
Triangle t1 = new Triangle(1, 2, 3);

findArea(s1); //Find square area
findArea(c1); //Find circle area
findArea(t1); //Find triangle area

Much easier right?


As for the challenge, for part 2 it specifies that in the new overloaded method you just have to call the drive method from earlier and pass it the parameter 1. I will leave that for you to figure out, though let me know if you run into issues.


As per how java accomplishes method overloading. Well that is way outside the scope of this class and you current knowledge level. That dives into compiler theory, and honestly I would have to spend a long time trying to explain how it works.

Here are a couple links though if you are interested:

Wikipedia Stack Overflow Java Optional Parameters Operator Overloading

Hope this post was helpful!

Great example thanks for clearing that up, i will look further into the links!