classes

matthew reid

Ranch Hand

Posts: 38

posted 5 months ago

hey everyone,

im trying to understand something about classes better. not exactly sure how to explain exactly but ill give it a go.
just say i have a class named property which has variables such as street, size, toilets, bed rooms and so on, then say i have another class named sales, which will include owner name, phone, sale price. what i want is to join them together. i understand how this could be done by sales extending property and using tostring and super.tostring. but can i join them without doing it this way.

hopefully someone can understand what im talking about

i have made 3 classes. the super class property and 2 sub classes of salesoffer and purchaseoffer. but the problem is salesoffer isnt a property and purchaseoffer isnt a property. everything works the way i want it to work but i know its the wrong way to do it.

So a Sale (Sale, not sale, Sale not Sales) “IS‑A” Property (rather than property)? Surely a Sale “HAS‑A” Property?
And why would a Sale have an owner's name? Surely a Sale would have a seller, a buyer and a property field? And a price field. The classes for buyer and seller would have name fields.

matthew reid

Ranch Hand

Posts: 38

posted 5 months ago

this is what i got

im assuming the seller is the owner. i was going to make a super class of People, then have sub classes coming from that of Agent, and Client, then Buyer and Seller as sub classes of Client.

Campbell Ritchie

Marshal

Posts: 56546

172

posted 5 months ago

Person class, not people. Why do you need subclasses of Person?
What makes you think a SaleOffer “IS‑A” Property?
Don't use names like finalize because there is a method of that name, which you probably don't want to use, but there is a risk of confusion. I suggest you choose a different name for the boolean.

Has somebody told you that you must use subclasses?

matthew reid

Ranch Hand

Posts: 38

posted 5 months ago

i was going to do the sub classes because i thought that was how things are done. if i was to make this program for real use i would want each client and real estate agents stored in the database.

the reason im having so many problems is because my teacher is useless. and im doing this as distance education.

i was aware of the finalised method. im starting from scratch in my java book again to try get other things to sink in better and hopefully i will understand things differently now

Campbell Ritchie

Marshal

Posts: 56546

172

posted 5 months ago

Go and find yourself a copy of Object-Oriented Software Construction by Bertrand Meyer. He says things like, how can you write object‑oriented code without multiple inheritance? Then have a look at the date on the book. Now look at the sort of thing people are saying nowadays in this post for example. Opinions have changed about inheritance.
You have to be able to hold your hand on your heart and say that a Sale IS‑A Property and also that all Sale methods will work as Property methods would (Liskov substitution principle). Now, I would hold my hand on my heart and confidently say that a Sale ISN'T‑A Property, so I think there is something wrong with
public class Sale extends Property ... Also why are you putting name and address in the Sale object (or Offer object)? The name and address belong to a Person. A name doesn't make an offer, but a Person might. Which is why I thought you would have the Person as a field in the Sale object. You can consider having other classes, e.g. Offer and Bid. If I remember correctly, an offer means showing something as being for sale and a bid is an attempt to buy it by giving money. You can have multiple offers and bids and choose between them and even withdraw them before a certain stage is reached. Once you have bade for an offer and it is accepted, you have a sale. You might do well to simplify things by missing out half the fields. A contract only needs four fields: buyer, seller, property, and price. You can add agents and commission later. Get something simple written and let us have a look at it.

matthew reid

Ranch Hand

Posts: 38

posted 5 months ago

ok i changed the class names and changed the variables, removed the extends, made a Person class, would i be correct to made a sub class for Person called Agent and put the variables for commission.

here is what i got

Dave Tolls

Ranch Foreman

Posts: 3061

37

posted 5 months ago

Presumably a Sale and an Offer are on a Property, so they might need a Property attribute?

1. Line 4 comment says "variables". When you go to shop and give money to sales person, do you say, "money"? Or is it some kind of obvious?
2. Line 5. "num" and the comment next to it. If you catch yourself that you came up with a variable name which requires an explanation what it represents - it is a first out of one required sign, that variable name isn't descriptive. Consider changing it to "propertyNumber".
3. Similar issues with other variable names (all of them).

In line comments are for the purpose to explain things, which you cannot express yourself with the variable names. i.e.:

4. Why there is an empty line 3?
5. Why comment at line 5 not aligned as other comments? (deleting comments supposed to resolve that issue).