So let's suppose that I have a class Animal in Java, and 3 child classes. The structure looks like this
class Animal{
...
public breed(){
//create a new child class object here
}
}
class Bird extends Animal{
...
}
class Bear extends Animal{
...
}
class Frog extends Animal{
...
}
I want to create another object in breed class, but I want to create an object of the same child class, from which the method breed() was executed. For instance, if frog.breed() was executed I want to create a new Frog object there (assumming frog is a Frog object), if bear.breed() was executed I want to create a new bear object etc.
Is there any way to handle it in animal class, or I have to override method in every child class?