Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super class constructor

 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:

class Snack extends VendingMachinePoly {
private double cost;
private int calories;

// Parameterized constructor
Snack(double c, int cal) {
super(50, 50.0, c);
cost = c;
calories = cal;

When I compile the program I get an error that states:

"Snack.java": cannot resolve symbol: constructor VendingMachinePoly (int,double,double)in class chapter9.VendingMachinePoly at line 26, column 5

However, the following is what I have in VendingMachinPoly for the constructor:

// non-Default Constructor, n is the # of items the machine starts with
VendingMachinePoly(int n, double d, double cost) {
itemCount = n;
moneyCredit = 0;
totalMoney = d;
itemCost = cost;

I have tried many combinations of "int, double, double" to pass to the constructor of super (VendingMachinePoly), but nothing appears to help. Can anyone help explain what the compiler is trying to tell me???

Thank you,
Mitch
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What access qualifier do you have on your superclass constructor? Are these two classes in the same package?
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used public and none for the qualifier (tried both). Neither worked. Yes they are both in the same package (chapter9).

Any thoughts?

Thank you,
Mitch
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't accidentally given the superclass constructor a return type, making it into a normal function that just looks like a constructor, have you?
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No return on the constructor. But, that was a good check.
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have another version of the class file 'VendingMachinePoly' (maybe an older one) somewhere in the classpath? Maybe that version is getting picked up first.
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not believe there was more than one VendingMachinePoly. I could not find more than one (searched the entire directory with windows explorer). Again, another really good ckeck. I would not have thought about that at all.

Thank you,
Mitch
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled it OK so the error is not in the code you posted.

I think maybe you should post a cut and paste of the full code. Please use the UBB button "CODE" to preserve indentation.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this were my bug, my next step would be to create a no-arg VendingMachinePoly constructor and see if super() worked. That would tell me where the problem was.

If super() worked, I would add one parameter at a time to see which one was causing the error.

If super() failed, I would put both classes in one compilation unit (only one class can be public) to eliminate classpath isues.
Good luck!

Please reread this post. I fixed some errors.
[ November 21, 2004: Message edited by: Mike Gershman ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic