aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Navigable properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Navigable properties" Watch "Navigable properties" New topic
Author

Navigable properties

René van der ven
Greenhorn

Joined: Oct 25, 2009
Posts: 7
Hi Cameron,

Thanks for your great book. By far better than the Mc Graw Hill. I know because I have read that one first.
Especially the Association part. I did not find that in "Head First 5" and not in "Mc Graw Hill". So keep doing the good work!

Concerning page 134.

Example is about
Customer(name, gender, income age, Address, getAddress, setAddress)
Address(street, city, state, country)

If I extend your example with Supplier
Where do I place the getAddress and setAdress methods.

Supplier(name, gender, income age, Address, getAddress, setAddress)
or
Address(street, city, state, country, getAddress, setAddress)

If I choose the first: I do have twice the same methods in different classes. Is this correct?
When do I place the methods in the Supplier class and when do I place those methods in the Address class?


Thanks in advance for your reply.

Rene'
Keith Wegner
Greenhorn

Joined: Jan 21, 2009
Posts: 10
René van der ven wrote:
Example is about
Customer(name, gender, income age, Address, getAddress, setAddress)
Address(street, city, state, country)

If I extend your example with Supplier
Where do I place the getAddress and setAdress methods.

Supplier(name, gender, income age, Address, getAddress, setAddress)
or
Address(street, city, state, country, getAddress, setAddress)



Think about this conceptually.

Does a Customer have an address? In literal terms, does he have a physical residency that something is being delivered?
Does a Supplier have an address? In literal terms, does he have a physical place of business that he sends his products from, or that a Customer sends his payment to?

The answer to both of those is Yes.

And would it make sense for an address to getAddress() or setAddress()? You're asking the class to set itself and get itself.
An Address would have methods like:

public void setStreet(String streetName){...}
public String getStreet(){...}

public void setZipCode(String zipCode){...}
public String getZipCode(){...}


Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

You'll quite often see two classes with the same sets of methods. That's completely normal, and completely correct. After all, a person and a supplier might share the same address. Both point to the same data in memory, so only the methods are duplicated, but the data is all stored in the same place, so it's efficient.

You wouldn't put set and get Address in the Address class. The address already knows about itself. It's others that use it that need to updated it and perhaps change it.

-Cameron McKenzie
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Navigable properties
 
Similar Threads
My last 2 Qs about CMR please
Calling a method from within a method in another class (in the same package)?
Create this method ?
Generating Class Files from an XML Doc
Hook class