I am looking for some advice on how I could approach the following scenario:
Let's say I have an array of a type and in that array there are objects of various types that extend said type. For example: I have a super class Pet, I make a Pet array, and then put various objects of Dog, Cat, and Fish classes and put them in this array. Now, each type of pet has its own fields and methods, naturally.
This is where my issue comes into play. I want to loop through this array and search for a specific object and call one of its methods. Building off my previous example, I want to search for a Dog by the name of "Benny". Once I find it, I want to call Benny's getHairLength
method. However, if I'm iterating through my collection of various Pet types, I cannot call Pet[index].getHairLength
since not all pets have this method(only Dogs do).
Any tips?