Results 1 to 6 of 6
Thread: Classes with object attributes
- 11-24-2008, 07:08 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
[Solved] Classes with object attributes
Okay, I have no idea what to call this problem, I am pretty new to Java and need some assistance.
I have an inventory program I am building for a school project and am running into an issue trying to provide data to objects. This program is supposed to use static data and output each item in a stock/inventory kind manner. I have three files, Main, Product, and CD. Essentially, the Product class is supposed to be movies, and the CD class is for CDs. At first I was supplying class variables in each class file and just using that data, but I am now trying to provide attributes to the objects in the Main file. Like this:
The problem is I am receiving a "cannot find symbol" error. Symbol: constructor Product(). This is occuring on the line indicated by the comment. I have done some debugging and found that if I remove the public Product() portion of code from my Product.java class then this error goes away. Why is this occuring?Java Code:// create our inventory objects private static Product items[] = { new CD("Staind - The Illusion of Progress", 102, 6, 12.99, "Rock", "CD"), new Product("Boondock Saints", 101, 4, 14.99, "DVD") };
Product.java
CD.javaJava Code:public class Product { private String prodName; private int itemNumber; private double stock; private double price; private String prodType; public Product(String name, int number, double inStock, double itemPrice, String type) { prodName = name; itemNumber = number; stock = inStock; price = itemPrice; prodType = type; } // methods here }
Java Code:public class CD extends Product { private String prodName; private int itemNumber; private double stock; private double price; private String genre; private String prodType; public CD(String name, int number, double inStock, double itemPrice, String itemGenre, String type) { // error is occurring on this line prodName = name; itemNumber = number; stock = inStock; price = itemPrice; genre = itemGenre; prodType = type; } // methods here }Last edited by RRasco; 11-24-2008 at 09:22 PM. Reason: Solved!
-
If CD inherits from Product, I think that you shouldn't give CD the same fields that Product already has. You should only give CD fields that it has that are not duplicated in Product.
Your error is due to your CD constructor's not explicitly calling the product constructor -- super(....) on the first line of the CD constructor. If you don't do this, the compiler will try to call a default Product constructor, one with no parameters, and since this doesn't exist an error is produced.
The solution: 1) get rid of the redundant fields in CD and only keep fields that are new to CD and not in product.
2) The first line of your CD constructor should be a call to super(...) the Product's constructor. And make sure to pass all the necessary parameters to this constructor.Last edited by Fubarable; 11-24-2008 at 07:55 PM.
- 11-24-2008, 09:22 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
Awesome! I was able to solve this based off of your suggestions. The subclass did in fact need the parameters, but I needed to use super() on the inherited ones.
Thanks!
Java Code:public class CD extends Product { private String prodName; private int itemNumber; private double stock; private double price; private String genre; private String prodType; public CD(String name, int number, double inStock, double itemPrice, String type, String itemGenre) { super(name, number, inStock, itemPrice, type); prodName = name; itemNumber = number; stock = inStock; price = itemPrice; prodType = type; genre = itemGenre; } // methods here }Last edited by RRasco; 11-24-2008 at 09:24 PM. Reason: Syntax correction
-
I'm still thinking that you don't need these parameters.The subclass did in fact need the parameters
Usually you're able to get a handle on these guys through the super class's public accessor and mutator methods.
Please come back after this has been graded and let us know what your instructor thinks.Last edited by Fubarable; 11-24-2008 at 11:34 PM.
- 11-24-2008, 11:44 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
I agree that theoretically through inheritance, no I should not need them. However, when I remove them it throws an exception. I was able to remove the declarations and initializations, but I had to keep them supplied as attributes.
I would come back and share what my instructor says, but he is less than competent when it comes to providing feedback and answering questions, hence why I am here to begin with.Java Code:public class CD extends Product { // setup our class attributes private double inStock; private double itemPrice; public CD(String name, int number, double stock, double price, String type, String genre) { // call the super constructor super(name, number, stock, price, type, genre); // assign attributes inStock = stock; itemPrice = price; } // methods here }
Thanks anyways mate! This works and I'm sticking with it. However, if you would like to assist me further I am supposed to create a GUI to display the output as opposed to using System.out.println(). If you could recommend a GUI component I would appreciate it much!Last edited by RRasco; 11-24-2008 at 11:48 PM.
-
As a general rule, it's never good to fix a bug with a kludge but rather to find out why the bug occurs in the first place, but I'm sure that I'm not telling you anything that you don't already know.I agree that theoretically through inheritance, no I should not need them. However, when I remove them it throws an exception. I was able to remove the declarations and initializations, but I had to keep them supplied as attributes.
Regarding GUI's I'd recommend you search this and other forums for ideas and sample code. There's a lot of code out there, some of it even is good.
Similar Threads
-
JTextPane (reading char and its attributes)
By Defero in forum New To JavaReplies: 0Last Post: 07-19-2008, 08:09 PM -
How to read AIX attributes & values.
By John_28 in forum New To JavaReplies: 5Last Post: 05-05-2008, 08:07 AM -
getting attributes from one class to another in 2 different .java files
By Mr tuition in forum Java ServletReplies: 2Last Post: 12-04-2007, 10:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks