Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Variable static in subclass

  1. #1
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Variable static in subclass

    Hello everybody

    I have a little problem

    I wrote this abstract class UNIT

    Java Code:
    public abstract class Unit {
    	public static enum TYP {
    		Archer, Blader, Blaster, Bomb, Gunner, Heavy_Armor, Helicopter, Robot, Spear, Vehicle
    	}
    
    	public Unit(){
    		
    	}
    }
    and this two sub classes

    Java Code:
    public class Archer extends Unit {
    	private static Vector<LEVEL> level = new Vector<LEVEL>();
    	private static int levelIndex = 0;
    	private static UPGRADE upgrade = new UPGRADE();
    
    	public Archer() {
    		setData(getLvl(), getUpgrade());
    		setTyp(TYP.Archer);
    	}
    	
    	public Archer(Point UnitCoordinate) {
    		super(UnitCoordinate);
    		setData(getLvl(), getUpgrade());
    		setTyp(TYP.Archer);
    	}
    
    	public static void setLevels(Vector<LEVEL> level) {
    		Archer.level = level;
    	}
    
    	public static void setLevelIndex(int index) {
    		Archer.levelIndex = index;
    	}
    
    	public static void upgrade(int UI) {
    		upgrade.upgrade(UI);
    	}
    
    	public static LEVEL getLvl() {
    		return level.elementAt(levelIndex);
    	}
    
    	public static UPGRADE getUpgrade() {
    		return upgrade;
    	}
    }
    and
    Java Code:
    public class Blader extends Unit{
    	private static Vector<LEVEL> level=new Vector<LEVEL>();
    	private static int levelIndex=0;
    	private static UPGRADE upgrade=new UPGRADE();
    	
    	
    	public Blader(Point UnitCoordinate) {
    		super(UnitCoordinate);
    		setData(getLvl(), getUpgrade());
    		setTyp(TYP.Archer);
    	}
    	
    	public static void setLevels(Vector<LEVEL> level){
    		Blader.level=level;
    	}
    	
    	public static void setLevelIndex(int index){
    		Blader.levelIndex=index;
    	}
    	
    	public static void upgrade(int UI){
    		upgrade.upgrade(UI);
    	}
    	
    	public static LEVEL getLvl(){
    		return level.elementAt(levelIndex);
    	}
    	private UPGRADE getUpgrade() {
    		return upgrade;
    	}
    }
    so my question is can i generate all methods of Archer and Blader in Unit and return different variables for each subclass?
    Last edited by alexanderschill; 01-01-2013 at 02:12 AM. Reason: False Question

  2. #2
    Nouish is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Variable static in subclass

    Have abstract methods in the abstract class Unit

    Java Code:
    public abstract void setLevelIndex(int index);
    You must now override these in subclasses, you do not need to have the Override annotation:

    Java Code:
    @Override
    public void setLevelIndex(int index) {
    	// do stuff
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Variable static in subclass

    Quote Originally Posted by alexanderschill View Post
    Hello everybody

    I have a little problem

    I wrote this abstract class UNIT

    Java Code:
    public abstract class Unit {
    	public static enum TYP {
    		Archer, Blader, Blaster, Bomb, Gunner, Heavy_Armor, Helicopter, Robot, Spear, Vehicle
    	}
    
    	public Unit(){
    		
    	}
    }
    Your base abstract class won't be very helpful in your situation unless you give it methods, likely abstract ones.


    and this two sub classes

    Java Code:
    public class Archer extends Unit {
    	private static Vector<LEVEL> level = new Vector<LEVEL>();
    	private static int levelIndex = 0;
    	private static UPGRADE upgrade = new UPGRADE();
    
    	public Archer() {
    		setData(getLvl(), getUpgrade());
    		setTyp(TYP.Archer);
    	}
    	
    	public Archer(Point UnitCoordinate) {
    		super(UnitCoordinate);
    		setData(getLvl(), getUpgrade());
    		setTyp(TYP.Archer);
    	}
    
    	public static void setLevels(Vector<LEVEL> level) {
    		Archer.level = level;
    	}
    
    	public static void setLevelIndex(int index) {
    		Archer.levelIndex = index;
    	}
    
    	public static void upgrade(int UI) {
    		upgrade.upgrade(UI);
    	}
    
    	public static LEVEL getLvl() {
    		return level.elementAt(levelIndex);
    	}
    
    	public static UPGRADE getUpgrade() {
    		return upgrade;
    	}
    }
    and
    Java Code:
    public class Blader extends Unit{
    	private static Vector<LEVEL> level=new Vector<LEVEL>();
    	private static int levelIndex=0;
    	private static UPGRADE upgrade=new UPGRADE();
    	
    	
    	public Blader(Point UnitCoordinate) {
    		super(UnitCoordinate);
    		setData(getLvl(), getUpgrade());
    		setTyp(TYP.Archer);
    	}
    	
    	public static void setLevels(Vector<LEVEL> level){
    		Blader.level=level;
    	}
    	
    	public static void setLevelIndex(int index){
    		Blader.levelIndex=index;
    	}
    	
    	public static void upgrade(int UI){
    		upgrade.upgrade(UI);
    	}
    	
    	public static LEVEL getLvl(){
    		return level.elementAt(levelIndex);
    	}
    	private UPGRADE getUpgrade() {
    		return upgrade;
    	}
    }
    so my question is can i generate all methods of Archer and Blader in Unit and return different variables for each subclass?
    Parent classes don't inherit from the child classes but rather visa-versa -- the child can only inherit from the parent, and in fact the parent class should have no knowledge of or dependence on the child class. Also none of the methods and variables above should be static as it completely defeats the purpose of the methods -- to set and get an object's fields. Also statics cannot take advantage of polymorphism.

  4. #4
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Variable static in subclass

    But if i have the Levels etc. not static i can only acess them from an instance of the child, and this variables arent constants for the whole class thats my problem

    I need to set the levels for all childs of class blader or archer and acess them without an instance

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Variable static in subclass

    Quote Originally Posted by alexanderschill View Post
    But if i have the Levels etc. not static i can only acess them from an instance of the child, and this variables arent constants for the whole class thats my problem

    I need to set the levels for all childs of class blader or archer and acess them without an instance
    For an identifier/variable being static doesn't imply that it is constant. Java has the 'final' qualifier for that. Overall: your last reply doen't make much sense and I suggest you redesign your classes because the requirements are a mess now ...

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 01:00 PM
  2. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  3. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  4. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •