When you're in the Contact class, as you are in the examples you've posted, this refers to the instance of the Contact class you're in at the time. So, for example, this.setAddress(street, city, zip) is trying to call setAddress(street, city, zip) on the Contact object. Which doesn't have that method.
You want to call it on the Address object instead. That means you have to call it via Address object reference - contactAddress, in this case.
Alternatively, you could create a new Address object and change contactAddress to point to it.
J Kough wrote:I would like to overload this, and create a mutator method that accepts three Strings, for the streetAddress, city, and postalCode.
I'm not quite sure why. All it saves you is a few characters when calling, viz:
setContactAddress(street, city, zip); as opposed to:
setContactAddress(new Address(street, city, zip));
This is what I'm trying to add within the Contact class, but it's not working:I can't seem to access the public method setAddress (from the Address class) from within a method in the Contact class. Is this possible?
Or how else might I be able to accomplish this?
If you really feel you have to, I'd do:but, as I say, it seems a bit redundant to me.
Clean APIs generally don't have a lot of overloaded methods, and you can easily get sidetracked from your primary goal by creating lots of "convenience" methods.
My advice: keep to methods that are strictly required for the business logic for the moment.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here