[Solved] How to access an object (already cast as its parent) as its actual object without reflection?


You don’t have to use reflection at all. If you understand what type it is you like, you can use instanceof to get the specific class instance you care about.

for(WordCategories.Noun n: nouns) {
  if(n instanceof WordCategories.Person) {
      // cast to WordCategories.Person and perform whatever action you like
      WordCategoriesPerson actualPerson = (WordCategories.Person) n;
  }
}

This trumps the usage of the field to determine the object type, since the class contains enough metadata for you to want to use in this scenario. While many people would also discourage the use of instanceof due to performance (and frankly, if you wanted a list containing WordCategories.Person, just ask for one), its use in this instance would be cleaner than forcing each child class of WordCategories.Noun to create a method to inform us of what type it is.

1

solved How to access an object (already cast as its parent) as its actual object without reflection?