Reputation: 13
So I have a question about interfaces after experimenting for a bit: Can specific information be put into one?
Here's my interface:
public interface Weapon{
public int damage();
public int range();
public String name();
public int rateOfFire();
public String[] applicableOperators();
public void getSpecs(); //this is the method in which I have a question
}
Next, I implement it into the given gun: the R4C.
public class R4C implements Weapon{
@Override
public int damage(){
return 34;
}
@Override
public int range(){
return 20; //meters
}
@Override
public String name(){
return "R4-C";
}
@Override
public int rateOfFire(){
return 850; //rounds per minute
}
public String[] applicableOperators(){
String[] appOps = new String[1];
appOps[0] = "Ash";
return appOps;
}
public void getSpecs(){
System.out.printf("Name: %s\nDamage: %d\n Rate of Fire: %d\n"
+ "Range: %d\n", name(), damage(), rateOfFire(), range());
System.out.println("\nApplicable Operators:");
for(int arrayCount = 0; arrayCount < applicableOperators().length; arrayCount++){
System.out.printf("%s\n", applicableOperators()[arrayCount]);
}
}
}
So I want to have the "getSpecs" method ALWAYS have the same output. No matter which gun is being put in, I always want this same format:
Name: (WeaponName)
Damage: (Damage)
Rate of Fire: (RoF)
Range: (Range)
Applicable Operators:
Ash
Is there a way to do this inside an interface? Or would I just have to make a seperate method meant for output somewhere else? I know this is probably a basic question, but because I understand so little of interfaces so far, any input could help. Thanks!
Upvotes: 0
Views: 48
Reputation: 1260
If you want to archive this kind of function I recommend abstract class
instead of interface
public abstract class Weapon {
protected int damage;
protected int range;
protected int rateOfFire;
protected String name;
abstract public int damage();
abstract public int range();
abstract public String name();
abstract public int rateOfFire();
abstract public String[] applicableOperators();
abstract public void getSpecs(); //this is the method in which I have a question
}
public class R4C extends Weapon {
@Override
public int damage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
@Override
public int range() {
return range;
}
public void setRange(int range) {
this.range = range;
}
@Override
public int rateOfFire() {
return rateOfFire;
}
public void setRateOfFire(int rateOfFire) {
this.rateOfFire = rateOfFire;
}
@Override
public String name() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] applicableOperators() {
String[] appOps = new String[1];
appOps[0] = "Ash";
return appOps;
}
public void getSpecs() {
System.out.printf("Name: %s\nDamage: %d\n Rate of Fire: %d\n"
+ "Range: %d\n", name(), damage(), rateOfFire(), range());
System.out.println("\nApplicable Operators:");
for (int arrayCount = 0; arrayCount < applicableOperators().length; arrayCount++) {
System.out.printf("%s\n", applicableOperators()[arrayCount]);
}
}
}
Now you can change value at run time easily using setXXX
methods
Upvotes: 1
Reputation: 4420
in java>=8 you can define default method in interface :
public interface Weapon{
public int damage();
public int range();
public String name();
public int rateOfFire();
public String[] applicableOperators();
default public void getSpecs() {
//this is the method in which I have a question
System.out.printf("Name: %s\nDamage: %d\n Rate of Fire: %d\n"
+ "Range: %d\n", name(), damage(), rateOfFire(), range());
for(int arrayCount = 0; arrayCount < applicableOperators().length; arrayCount++){
System.out.printf("%s\n", applicableOperators()[arrayCount]);
}
}
}
Java 8 introduces “Default Method” or (Defender methods) new feature, which allows developer to add new methods to the interfaces without breaking the existing implementation of these interfaces. It provides flexibility to allow interface define implementation which will use as default in the situation where a concrete class fails to provide an implementation for that method.
Upvotes: 2
Reputation: 432
Perhaps you could user interface default methods. You can read more about this here.
Upvotes: 1