• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java Question (On abstract classes) (Problem solved)

Joony

Diamond Member
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);
}
}
 
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
 
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.
 
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.
 
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.
 
hmm, wow, it worked even without making cost protected...

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

 
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.
 
Back
Top