Here is the gist of what I am trying to do:
abstract class Animal {
abstract static Animal fromInput(String input); // <- error
static List<Animal> makeFive() {
List<Animal> animals = new ArrayList<Animal>();
animals.add(Animal.fromInput("Input 1"));
animals.add(Animal.fromInput("Input 2"));
animals.add(Animal.fromInput("Input 3"));
animals.add(Animal.fromInput("Input 4"));
animals.add(Animal.fromInput("Input 5"));
return animals;
}
// ^^^ how to this rewrite this so Dog.makeFive() ^^^ //
// ^^^ makes Dogs and Cat.makeFive() makes Cats? ^^^ //
}
class Dog extends Animal {
@Override static Dog fromInput(String input) {
Dog dog = new Dog();
// do some initial dog stuff with input
return dog;
}
}
class Cat extends Animal {
@Override static Cat fromInput(String input) {
Cat cat = new Cat();
// do some initial cat stuff with input
return cat;
}
}
How to write this correctly?
I want to be able to call Dog.makeFive()
to give me a List<Dog>
or Cat.makeFive()
to give me a List<Cat>
without having to re-define makeFive()
in Dog
, Cat
, and every other animal class.
EDIT: I know the use of static
within an abstract
class is wrong. My question is how to work around that so one is able to call Dog.makeFive()
or Cat.makeFive()
while only defining makeFive()
once.