![]() |
| ||
| Inheritance vs New Method I want to learn more about how inheritance works, more like I NEED to learn more about it if I am to get any better at this. In my code I have the value of a cd defined as how many cds are in stock times the price of the cd. I did this in my first class Compactdisk. Now I have created a sub class that adds a new parameter in to my array, and that all makes sence to me now. What if I want to alter the output of a method I have already done though? Value for instance. With my new subclass I want to make Value mean (price X stock X restock) where restock is going to be a 5% restocking fee. I should not have to alter Compactdisk, as CdwArtist is a sub and inherits everything, so I added what you see below to CdwArtist. I think it is right, but how would I then get Inventory to grab this new Value instead of the old Value? import java.util.*; My first class: import java.lang.Comparable;and my new sub class, with my added code in bold. import java.util.*; I was hoping that I could just add a new method with the same name, and since everything is just inherited that my Inventory class would take the last inherited value for Value. It did not work. I still only get the original value that I set up in Compactdisk. Am I making any sense? |
| ||
| Re: Inheritance vs New Method I'll give you a simple example.. Imagine a class: public class Animal{Now we can inherit an 'Animal' and 'override' its noise method. public class Dog extends Animal{Dog inherits all the functionality of Animal and customises the noise method. This is approaching another subject (Polymorphism). |
| ||
| Re: Inheritance vs New Method I do not see any structural problems there. You have overridden the method correctly. There are other issues with the code though and it will not compile as you have it posted. You have defined "restock" as an int, assigned it a double value in the constructor, and also left off the semicolon on the assignment. You will want to make restock a float or double value for your math to work correctly. |
| ||
| Re: Inheritance vs New Method I am so stupid. The file I was running was not the file I had compiled. I was coding and compling in one file, running the other and wondering why nothing changed! :o) Once I did correct that issue it does work, if I take out ".05" and just put in "5" or "05". So my method was right! First time! woo hoo, I am so happy. Of course I need it to be ".05". When I put the decimal back in it erros and says possible loss of percision, found float, double required. If I change it to doube is says it found double, float required. Here is what it looks like with the syntax corrected. public String artist; // artist performing on cd This is finally fun. I feel like am learning stuff and working through NEW problems for a change! |
| ||
| Re: Inheritance vs New Method The compiler is warning that you are mixing float and double calculations, which affect precision. It you don't want to worry about figuring out how to apply casting in your math expression, just change the type of restock to float and assign it "0.05f". The "f" denotes that it is a float, instead of double which is the default for decimals that you type in as literals. |
| ||
| Re: Inheritance vs New Method That is a neat trick, thanks for that tip. Of course it showed me that my math was even more wrong than I though. I had to change the formula to this: return(price * nstock)+((price * nstock)* restock); but with that I am going to say it is close enough, TheGather already pointed out the issue with the counter, so I have nothing more I want to do to this. I am taking the weekend off to catch up on some reading. I was hoping to start some GUI stuff next week, and that was my whole reason for starting this endeavor. I hope it is as fun as I originally thought it was going to be. Thanks so much for all your help this week guys. :icon_smile: |
| All times are GMT -4. The time now is 8:00 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC