1

I have a lot of classes that extend an abstract base class. I would like to automatically create an instance of each one and add them into an array list of base class objects, so when I need to iterate through the list I can call the same method and do something else depending on what derived class the current object is. The problem is how to instantiate everything automatically as the number of derived classes can be high, about 40-50, maybe more afterwards. Any help would be appreciated.

EDIT: I added a commentary in which I explained more details about the question

Andrew
  • 1,109
  • 1
  • 15
  • 27
  • I have no idea, why you want to do this, but if you make the effort of creating 50 or so subclasses, it should be not so much more effort to make one more line of code to add an instantiation. – Fildor Sep 10 '14 at 09:31
  • Actually it's not the same question as i don't want all classes that extend the base class to be instantiated. I want only the ones that are in some package or directory. And the answer Fildor gave me isn't technically what I want. I need to read those objects from a json but I need to create them if the file doesnt exist. And need to check if any are null after I read them and create the ones that remained null. – Andrew Sep 10 '14 at 09:46
  • 1
    add your comment to the question then (by editing the question) – benka Sep 10 '14 at 10:17
  • Ok, let's assume you can instantiate all those classes from a certain package or path. Why not instantiate all and then replace those that can be deserialized from json? Is instantiation very expensive? – Fildor Sep 10 '14 at 10:20

0 Answers0