0

I'm making a game with different types of building. I'm making an interface for each type. Some buildings have more than 1 type.

I have this code:

public interface DefenseBuilding {
    int range;
    int damage;
    public void shoot ();
}

It gives me an error on the 2 variable declarations (range & damage). The error being something along the lines of "Final variable may not be initialised"

It works if I assign the variable in the interface, but I don't want to do that.

I can't just extend a class, because - as said earlier - some buildings need more than 1 type. Classes can only extend 1 other class so I need to use interfaces.

What I'm asking is, is there a way to have variables in an interface without having to initialise the variable inside the interface?

9
  • 4
    how to create variables in java interfaces: ...............don't. Either use an abstract class if you absolutely need fields, use the interface and let the concrete classes declare their own fields. Commented Nov 17, 2015 at 20:43
  • 3
    You cannot define dynamic field in interfaces, only static ones. You could instead define getters instead of dynamic fields. But I agree with @HovercraftFullOfEels 's approach to create an abstract class. Commented Nov 17, 2015 at 20:43
  • @HovercraftFullOfEels I already said, I Can't use classes - even abstract classes - as I need classes to be able to implement (or extend in classes case) them more than once. Commented Nov 17, 2015 at 20:47
  • @Turing85 Would a static interface act the same way as a static class, as in keeping the variables the same between all classes that implement it? Commented Nov 17, 2015 at 20:48
  • 2
    Zac -- OK, then it's very clear that what you're trying to do -- make an interface into an abstract class -- is not going to work. Commented Nov 17, 2015 at 20:48

4 Answers 4

2

The whole point of interfaces is to specify as interface - i.e. how will your classes interface with client classes. Instance variables are clearly not part of any interface at all.

Instead, try this:

public interface DefenseBuilding {
  public void shoot ();
}

and this:

public abstract class AbstractDefenseBuilding implements DefenceBuilding {
  protected int range;
  protected int damage;
}

edit:

Your classes should now extend AbstractDefenseBuilding, so they will inherit the variables. They also indirectly implement DefenceBuilding so they'll still be forced to implement the shoot() method (unless they are also abstract)

Sign up to request clarification or add additional context in comments.

7 Comments

I suppose that could work, I'll try it. Although, it will be a bit annoying to have to create the variables each time.
You won't have to - your classes will extend AbstractDefenseBuilding and inherit the variables.
What if I wanted to have a building which was a defence building and, say, a house?
I don't know what a house is in your context.
A house would be another building type (Same as DefenseBuilding)
|
1

You can use a property method aproach.

public interface DefenseBuilding {
    public void setRange(int range);
    public int getRange();
    public void setDamage(int damage);
    public int getDamage();
    public void shoot ();
}

Then in your class

public MyClass implements DefenseBuilding{
    int range;
    int damage;
    public int getRange() {
        return range;
    }
    public void setRange(int range) {
        this.range = range;
    }
    public int getDamage() {
        return damage;
    }
    public void setDamage(int damage) {
        this.damage = damage;
    }

    public void shoot (){...}
}

1 Comment

That's probably the best way to do it.
0

All variables in Interface are static and final. Hence, unless initialized, compiler will keep giving an error that it is not initialized. This is enforced because interface cannot be instantiated and therefore any variable should be of static in nature and cannot be changed.

If your intention is to define class variables, do as NickJ suggested.

5 Comments

@HarryHarrison lookup, any variable in Java Interface are static and final by default. Hope you remove that down vote after you read some documentation.
@HarryHarrison here's some links for you: stackoverflow.com/questions/2430756/…
@HarryHarrison Interface fields are implicitly static and final, and while a "final variable" may seem semantically impossible, we abide by the rules of the JLS and nothing else. (I evened out the score)
@HarryHarrison I didn't ask you to upvote, just asked you to remove your downvote if you agree with me.
@Aragorn - something I would gladly do if SO let me.
0

Interfaces define behavior but not state (other than constants). Protected variables are a potential danger to proper encapsulation of data (an object should hide its data and provide access through methods unless there is a very compelling reason not to). An alternative would be the following:

public interface DefenseBuilding {
  public void shoot();
  public int getRange();
  public int getDamage();
}

It's also VERY common to provide an abstract class that partially implements the interface:

public abstract class AbstractDefenseBuilding implements DefensBuilding {
  private int range;
  private int damage;

  public AbstractDefenseBuilding(int range, int damage) {
    this.range = range;
    this.damage = damage;
  }

  public int getRange() {
    return range;
  }

  public int getDamage() {
    return damage;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.