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 (Retired) Harnessing the Power of Objects Method Signatures

Zachary Martin
Zachary Martin
3,545 Points

I don't understand the connection between the drive methods

I figured out the solution to the problem but I don't understand how and why it works.

When you have

public void drive(int laps) {

mBarsCount -= laps; }

Then you make the non parameter drive

public void drive() {

drive (1); }

How would the non parameter function know to subtract mBarsCount for each single lap?

2 Answers

andren
andren
28,558 Points

The non-parameter method is calling the drive method that takes 1 parameter within itself, providing it 1 as the lap argument, so when you call drive() the code within drive(int laps) will also get run with 1 as the lap argument due to the fact that it is called within the drive method.

Zachary Martin
Zachary Martin
3,545 Points

So thy're connected by way of the method name correct?