public class Buildings {
public int getLevel(){
return level;
}
}
public class MainBuilding extends Buildings{
public static final int cost = 200;
public static int level = 1;
}
MainBuilding main = new MainBuilding();
main.getLevel();
I want have several building with diffrent instantsvaribles like level and cost but they should have the same method getLevel() that just returns the level. I can't find how I can do these with java...
Either move the level
variable (which should probably be an instance variable, rather than a static variable) to Buildings
or make getLevel()
abstract so that each subclass has to implement it separately (whether that's by returning a variable, or just always returning a constant, or whatever - that's an implementation detail).
See more on this question at Stackoverflow