I have an Android app to assist in collecting data for animal observations. It has an activity ObserveAnimal and a model Animal with normal getters and setters.
I am creating a different flavor of the app with extended functionality and extended data for bird observations. ObserveBird extends ObserveAnimal and Bird extends Animal. ObserveBird adds some extra methods.
public class Animal {
private String species;
// there are more fields and public getters and setters ...
}
public class Bird extends Animal {
private int wingLength;
// public getters and setters ...
}
Problem is I can't find the right way to declare and initialize Bird. I tried several ways, like:
public class ObserveAnimal extends AppCompatActivity {
protected Animal a = new Animal();
}
public class ObserveBird extends ObserveAnimal {
protected Bird a = new Bird();
}
ObserveAnimal
and Animal a
work nicely together as they should.
However in ObserveBird
theBird a
declaration hides the Animal a
declaration and I end up with two different instances. Instance Animal a
gets its species
field set from the code in ObserveAnimal
and instance Bird a
gets its wingLength
field set from the code in ObserveBird
.
I am trying to have one instance of Bird a
in ObserveBird
with species
set from the code in ObserveAnimal
and wingLength
set from the code in ObserveBird
.
I want to have an instance Bird a
(and no other instance of Animal
) when I start activity ObserveBird
.
How can I do that without duplicating a lot of code from ObserveAnimal to ObserveBird?