- #1
Robben
- 166
- 2
Homework Statement
How can I return a new class object in an abstract class?
Homework Equations
None
The Attempt at a Solution
Suppose I have an abstract class named Animal (there are many other animal types that are made as classes). In this abstract class it has two methods, reproduction(Animal otherAnimal) and reproducing(Animal otherAnimal). Reproduction determines if the animal can reproduce with another instance of an animal. Reproducing should return a new Animal of the same type if reproduction is possible, but I do not know how to do this because the class is abstract.
Java:
public abstract class Animal{
public abstract boolean reproduction(Animal otherAnimal);
public Animal reproducing(Animal otherAnimal) {
if (reproduction(otherAnimal) == true) {
return ... //how can I return a new Animal of the same type?
}
else {
return null;
}
}
}