Hey,
I dont know where to start with this problem so I have a super class(Transaction) that has a constructor and I have 2 sub classes (rental and return )they both have the same constructor as the super but they have a toString method that is almost the same but not really . The rental has the id num ,format of the disc and the date and then the fee. while in the rent I have the id, format , fee. How can I write those method so I dont have duplicate codes and it easy to change and understand? I also want to get the total fee after the toString method at the end? can some one pleas help I would really be thankful !

Ok so in that problem they want you to use something called method overriding. What this means is as follows:

If a superclass (Transaction) has a method purchase(), then by default its subclasses (Rental, Return) will also have that same method. If I say:

Rental rent = new Rental();
rent.purchase();

Even if the purchase() method is not explicitly defined in the Rental class, it will know to look in the Transaction class for the method.

However, if you want the Rental class to do something different when purchase() is called, you "override" the purchase() method in the superclass. All this means is that you just write out the purchase() method in both classes. Then, if the same code above is called, the JVM automatically knows to use the Rental class' version of purchase() and not the Transaction class' version.

In the event that you do want to use the superclass' method, use the keyword super. For instance:

public class Rental extends Transaction
{
    //code here

    public void purchase()
    {
        super.purchase();  //calls the Transaction class' purchase method
        //more code here
    }
}

Hope this helps!

Delete this post please -- it resulted from a browser glitch.

Um is there a way to delete that second post? My internet was glitching and it posted it twice I think...

Um is there a way to delete that second post? My internet was glitching and it posted it twice I think...

Thanks alot but problem is that with the toString method the almost the same the have the id and the format and fee but the date in the rental is before get the fee so I have this code so far

public String toString()
{
output = super.toStringId() + stringDate + super.toStringFee();
return output;
}
public String toString()
{
	output = stringId + stringFee;
		
		return output;
}

while in the transaction I have the same thing but the fee is the base. and my question is how can I print out the total in the end just once and I dont know on how about going to. Where should I write and how can I write so it rights it once

That code and explanation was a little confusing, but basically again you would need to write a toString() method in your Rental class and if at any point you want to access the toString() string from the Transaction class just call super.toString(). So for instance, if you had a String called date and you wanted to print the date string on one line followed by the Transaction toString() then you would put the following method in your Rental class:

public String toString()
{
    return date+"\n"+super.toString();
}

Make sense?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.