Use variable class names instead of lots of if clauses?
I’m stuck right now and don’t know a simpler solution, maybe you can help me.
I have an interface called Animal and many Animal classes that implement it.
EDIT: The interface must be wrong:
public interface Animals {
Integer lifespan = 0;
public Integer getLifespan();
}
In a function, I get some random animal objects and I want to get its variables.
if (animal instanceof GuineaPig) {
lifespan = ((GuineaPig) animal).getLifespan();
age = ((GuineaPig) animal).getAge();
value = ((GuineaPig) animal).getValue();
}
if (animal instanceof Rabbit) {
lifespan = ((Rabbit) animal).getLifespan();
age = ((Rabbit) animal).getAge();
value = ((Rabbit) animal).getValue();
}
Now I need to add an if clause for each animal, there must be an easier way, right? What am I doing wrong?
Edit 2:
Complete interface and class:
public interface Animals {
final Integer id = 0;
Integer prize = 999999;
Integer value = 0;
Integer age = 0;
Integer lifespan = 0;
String[] colors = {
"c_bw", "c_w", "c_brw"
};
String name = null;
String finalColor = null;
public String[] getColors();
public Integer getPrize();
public Integer getId();
public Integer getLifespan();
public Integer getAge();
public Integer getValue();
public String getName();
public String setName(String animalName);
public String setFinalColor(String finalColor);
}
class GuineaPig implements Animals {
private final Integer id = 0;
private Integer prize = 10;
private final Integer difficulty = 0; easy
private final Integer licenceNeeded = 0;
private Integer value = 5;
private Integer age = 0;
private String[] colors = {
"c_bw", "c_w", "c_brw"
};
private String name = null;
private String finalColor = null;
@Override
public Integer getPrize() {
return prize;
}
public void setPrize(Integer prize) {
this.prize = prize;
}
public Integer getDifficulty() {
return difficulty;
}
public Integer getLicenceNeeded() {
return licenceNeeded;
}
@Override
public String[] getColors() {
return colors;
}
public Integer getId() {
return id;
}
@Override
public Integer getLifespan() {
return null;
}
public String getName() {
return name;
}
public String setName(String name) {
this.name = name;
return name;
}
public String getFinalColor() {
return finalColor;
}
public String setFinalColor(String finalColor) {
this.finalColor = finalColor;
return finalColor;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
class Rabbit implements Animals {
private final Integer id = 1;
private Integer prize = 15;
private Integer lifespan = 30;
private Integer difficulty = 0; easy
private final Integer licenceNeeded = 1;
private Integer value = 7;
private Integer age = 0;
private String[] colors = {
"c_b", "c_w", "c_br"
};
private String name = null;
private String finalColor = null;
@Override
public Integer getPrize() {
return prize;
}
public void setPrize(Integer prize) {
this.prize = prize;
}
public Integer getDifficulty() {
return difficulty;
}
public Integer getLicenceNeeded() {
return licenceNeeded;
}
@Override
public String[] getColors() {
return colors;
}
public Integer getId() {
return id;
}
@Override
public Integer getLifespan() {
return null;
}
public String getName() {
return name;
}
public String setName(String name) {
this.name = name;
return name;
}
public String getFinalColor() {
return finalColor;
}
public String setFinalColor(String finalColor) {
this.finalColor = finalColor;
return finalColor;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Solution
In your little code example, you can simply have the Animals
interface with getLifespan
(), getAge(), and getValue(
)
methods, and avoid conversions and if statements:
lifespan = animal.getLifespan();
age = animal.getAge();
value = animal.getValue();
You don’t show the definition of an interface, but depending on your problem, the Animal interface may already have all of these methods.
Edit:
Your Animals
interface (by the way, Animal
would be a better name) only defines getLifespan().
If you add additional methods to it (assuming they are available in all classes that implement this interface), you will be able to call them without casting.