- Mar 19, 2006
- 1,539
- 1
- 81
I hope I'll be using the right terminology here, so here goes. I have a superclass and its subclass. The superclass is VillainPlayer, and its subclass is Counter. The basic design's like this:
//////////////////////////////////////////////
public class VillainPlayer
{
private int score = 0;
public void act()
{
//the value of score changes
}
public int getScore()
{
return score;
}
}
//////////////////////////////////////////////
public class Counter extends VillainPlayer
{
public void act()
{
System.out.println(super.getScore()); //or even getX() without super, both yield same result
}
}
/////////////////////////////////////////////
Note: Act method is called over and over.
So the problem here is the bolded part. The print statement constantly prints a stream of 0's, no matter what the current value of score is in the VillainPlayer class. The score is constantly changing (adding 100 points every time the mole is hit). Now, getScore works perfectly well inside the VillainPlayer class (inside it's "home" class). However, when a subclass tries to use the getScore method, only 0's are returned instead of the actual int value of score itself.
What could be wrong?
//////////////////////////////////////////////
public class VillainPlayer
{
private int score = 0;
public void act()
{
//the value of score changes
}
public int getScore()
{
return score;
}
}
//////////////////////////////////////////////
public class Counter extends VillainPlayer
{
public void act()
{
System.out.println(super.getScore()); //or even getX() without super, both yield same result
}
}
/////////////////////////////////////////////
Note: Act method is called over and over.
So the problem here is the bolded part. The print statement constantly prints a stream of 0's, no matter what the current value of score is in the VillainPlayer class. The score is constantly changing (adding 100 points every time the mole is hit). Now, getScore works perfectly well inside the VillainPlayer class (inside it's "home" class). However, when a subclass tries to use the getScore method, only 0's are returned instead of the actual int value of score itself.
What could be wrong?