If I understand the question correctly, you want to call the correct implementation of the methods regarding the type of object you have. Therefore, if the animal is a cat, the report method should be called from the Cat class if you have code, as shown below
public static Animal getAnimal(int id){
First of all, as you said,
Obviously, cats and dogs have different attributes. A cat and a dog do not have anything other than the fact that they are animals.
Since subclasses do not have common functions, define Animal as an interface instead of a class, as follows
interface Animal{ public void generateReport(); }
And create a Cat & Dog like this
class Cat implements Animal{
Since the generateReport () method is defined in the interface, all classes implementing the interface must have generateReport ().
So, when you make such a call,
public static Animal getAnimal(int id){
The base object method will be called.
If you just want to know what the animal object refers to (returned from the getAnimal method, that is, Cat or Dog), you can check it as shown below
class Animal{ } class Dog extends Animal{ public String dogName = "Dog1"; } class Cat extends Animal{ public String catName = "Cat1"; } public class HelloWorld{ public static void main(String []args){