Searching for simplest solution where a User can order an item.

If someone purchases an item he will have one address. Which then is the invoice and shipping address. But he could add another address if the invoice address is different from the shipping address.

I wrote some code with two Classes Purchase and Address. Where Purchase has a one-to-many relation with Address. So far so good and that all works fine.

But now, I might have a confusion. I was thinking I could also create a AbstractAddress class. Where Purchase extends the AbstractAddress and Address also extends the AbstractAddress. Now if there is a new Purchase the Address will be part of Purchase and the relation (one-to-many) from Purchase to Address will only happen if the Purchase get's added one more Address.

Is that correct? Would that be better? Could anyone please shed some light on this?

Thank you


No, that's not correct.

If you make Purchase extend AbstractAddress, you are saying that a Purchase is some kind of specialized AbstractAddress, which is not. It's like saying that a Car is a specialized Windshield.

The logical relationship between a Purchase and an Address is one of composition, not one of inheritance.

Just add two address members to Purchase.


public class Purchase {

      private shippingAddress Address;
      private invoiceAddress Address;   

      public void setShippingAddress(Address address){
            set shippingAddress=address;

      public void setInvoiceAddress(Address address){
            set invoiceAddress=address;

      public Address getShippingAddress(Address address){
            return shippingAddress;

      public Address getInvoiceAddress(){
            return invoiceAddress;


| improve this answer | |
  • Maybe its a good idea to let both addresses point to the same address instance, initially? Preferably passed to the constructor. Then you can just get the shipping address without having to know if it is or isnt the same address as the invoice address. – Teimpz Aug 22 '16 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.