Java Question (On abstract classes) (Problem solved)

Joony

Diamond Member
Jan 17, 2001
7,654
0
0
Ok, so I'm playing with this new concept of "abstract". I made a very simple program that dosen't do much. I have run across an issue, I get a error message when I compile. "cannot resolve symbol" variable c in class french.

public class Test
{
public static void main(String args[])
{
System.out.println("bah");
Donut yum = new French(5);
yum.EatDonut();
}
}

public abstract class Donut
{
int cost;
public Donut(int c)
{
cost = c;
}
public abstract void EatDonut();
}

public class French extends Donut
{
public French(int c)
{
super(c);
}
public void EatDonut()
{
System.out.println("I am eating a French Donut at the cost of" + c);
}
}
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
You should be using "cost" not "c". cost is the instance variable, the scope of "c" is only the constructor.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
you havent decalred a variable called c,

System.out.println("I am eating a French Donut at the cost of" + c); <----- no var named c here in scope
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I edited my earlier post, but I'll say it again since a lot of time people don't re-read psots they already read:

You should be using "cost" not "c". cost is the instance variable, the scope of "c" is only the constructor.

Edit: I see I should go back and read things as this has already been solved.
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Originally posted by: Ameesh
you havent decalred a variable called c,

System.out.println("I am eating a French Donut at the cost of" + cost);

Look at whats bold

Also, might want to put in "cost of " + cost + " dollars"); You know, just for aesthetic purposes.
 

FeathersMcGraw

Diamond Member
Oct 17, 2001
4,041
1
0
Originally posted by: Ameesh

first make cost public or protected and then change c to cost

Changing the visibility of cost isn't necessary since all of the classes are in the same package. But I'd make it protected just out of principle.
 

Joony

Diamond Member
Jan 17, 2001
7,654
0
0
hmm, wow, it worked even without making cost protected...

so by "supering" c in French, it already knows cost from donut?

 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Joony
hmm, wow, it worked even without making cost protected...

so by "supering" c in French, it already knows cost from donut?

super(c) in French just calls the Donut constructor with c as an argument.