Is there a way to return different variations of an abstract class in a function without losing information to slicing?
As in, let's say I have an abstract class Dog, and for dog there are WildDog, DomesticDog, Puppy.
Is it possible to create a function that takes in one of these function and return them without slicing information?
Example:
// Returns the type of dog you currently have
Dog getCurrentDog() {
return this.dog;
}
void setDog(Dog dog) {
this.dog = dog;
}
Is it possible to pass in WildDog or PuppyDog to setDog and retain all the information in the respective classes. Say for an example PuppyDog has a drinkMilk() function that the other dogs don't have, but I still want to be able to access it.
And if possible, what would the Java equivalent be?
The solution I'm thinking of right now would be to have a getDog() for each of the instances of Dog that return the specific instance of the dog, whether it be Puppy or WildDog. That way, I can specifically access the code in each class.