title: How to pass parent class instance in place of child class instances in polymorphic functions?
body: <p>I have two classes <code>Event1</code> and <code>Event2</code>. At runtime, I want to create instance of <code>Event1</code> or <code>Event2</code> using java reflection and pass it as a parameter to another function. That function is a polymorphic function, which depending upon the input parameter type, invokes the corresponding functionality. </p>
  
  <p>Since I don't know what kind of child event instance I may need to create at runtime, I extend both child classes from 'Event' class and create a parent class instance. My question is, can I pass instance of 'Event' class as a parameter, which will be resolved at runtime to one of the child classes for polymorphic functions.</p>
  
  <p>Here is my code.</p>
  
  <pre><code>public void myFunc(){
  
      String qualifiedName = new String("events.Event1"); // Event1 info is actually obtained at runtime. 
  
      Class cls = Class.forName(qualifiedName);
      Event baseEvent = (Event) cls.getDeclaredConstructor().newInstance(); 
      // I want to create an instance like, Event ev1 = new Event1(); and pass it as a parameter.
  
      observer.update(baseEvent);  // I get compile time error because no update method takes `Event` class instance as parameter.
  }
  </code></pre>
  
  <p>Observer class that contains polymorphic functions.</p>
  
  <pre><code>public void update(Event1);
  public void update(Event2);
  </code></pre>
  
  <p>Can Java generics help here? Thanks.</p>