Java Polymorphism Woes

Associate
Joined
8 Nov 2005
Posts
56
I understand the basic concept of polymorphism but i'm really struggling to implement a particular feature in my code! :confused:

My class structure is as follows:

abstract class Transaction
- Soap extends Transaction
* doTransaction (method within Soap)

class Till
- Contains array to store transactions

So for example I can manually enter a transaction by putting the following code in to the Till class.

Code:
array[1] =  new Soap( 10 );
(10 bars of soap)

The doTransaction method asks the user for example how many bars of soap the customer wants. My question is how do I get the information from my doTransaction method in to the array!

Hard to explain so perhaps it would be easier to explain over msn if anybody wouldn't mind adding me.

Details in trust.

Thanks!
 
I suppose that makes sense but perhaps it is the naming of my methods that are confusing things. The reason this code needs to use polymorphism is because each 'product' could have different properties.

For example, the soap might only ask the user how many they want, but another product might be a membership product, for example a season ticket entry to somewhere and instead of asking for amount it asks for the season ticket id.
 
That's fine, the Till is still the key object when a customer pays. So the customer collects products into a basket/trolley, then each product is swiped at the till, then the customer pays the total.

Something like:
Code:
public class Till {
  public List<Transaction> items = new ArrayList<Transaction>();

  public void addItem(Transaction item) {
    this.items.add(item);
    item.doTransaction(this);
  }
}

You'll need extra methods for the rest, of course. (addPrice() or some such on Till)
 
This is where i'm getting confused, each of my extended classes need to contain information particular to that product. I need to collect this particular information from the user depending on which product they have selected. In my mind the questions can be stored within a method of the extended class, my current code:

Code:
class SwimEntryTicket extends Transaction {

	public int numberOfAdults;
	public int numberOfChildren;
	private String time;
	private float totalCost;

	CasualSwim ( int a, int c, String ti, float co ){

		super( "Swimming" );
		numberOfAdults = a;
		numberOfChildren = c;
		time = ti;
		totalCost = co;

	}

	public void doInput ( ) {

		System.out.print("\n\tNumber of adults: ");
		numberOfAdults = Input.readInt();
		System.out.print("\n\tNumber of children: ");
		numberOfChildren = Input.readInt();
		//System.out.print("\n\tTime: ");
		//int valCheck = Input.readInt();
		//System.out.print("\n\tCost: ");
		//int valCheck = Input.readInt();

 	}

}

Now I need to use the information gathered from that method to put it in an array declared within my Till class which handles payment and stuff.
 
Just remember.. everything goes through the till. So all information must go to the till. Instead of directly outputting the information with System.out.print*, return a string (that the till then uses) or send it to the till via a method on the till class.
 
Last edited:
Taking a step back, may I ask why Soap inherits from Transaction?
If you want the Soap object to represent a bar of soap then it's not a transaction and shouldn't inherit from it.

It looks like what you're actually describing is a transaction of bars of soap, in which case shouldn't the class be named SoapTransaction or similar?

That then brings me onto the point, why would you want a SoapTransaction in the first place?
Presumably a transaction can be made up of multiple items and your design doesn't seem to cater for this very well.
 
Surely you should have a class for products rather than each individual Product, lets face it how many unique properties can a bar of soap have that it requires.
 
I think the original problem is because the method doTransaction() is not public in the transaction class, only the soap class.

Because the till sees all the objects in the array as transaction objects it only knows about the methods already in the transaction class.

To solve this create a virtual method in the transaction class and override it in the soap class or cast the object in the array to a soap object and then access the method like this:

((soap)array[1]).doTransaction();

But as people have said above maybe your current program architecture needs a rethink. I just wanted to actually solve your problem (hopefully).
 
I think you need to go back and read about polymorphism some more as there isn't any in this code.
 
I think you need to go back and read about polymorphism some more as there isn't any in this code.

There is.. abstracting classes is a part of polymorphism. Subclasses behave differently depending on their state and desired behaviour, but adhere to a single interface.
 
Subclasses behave differently depending on their state and desired behaviour, but adhere to a single interface.

Yes thats part of it but I can't see it been used correctly here, does the Transaction class have a doTransaction(); method declared in it because the OP suggests it's defined within Soap? One of the ideas behind polymorphism is that classes are grouped together that share a similiar behaviour. I think the OP understands the idea but doesn't have the design quite right here.
 
I don't see why Soap is a subclass of Transaction; they share nothing in common. Surely a Till processes a Transaction which is made up of Products.
 
Last edited:
Back
Top Bottom