Overloaded methods that handle subclasses of some abstract class
<p>I have a generic <code>Person</code> class, and two types of people <code>Student</code> and <code>Teacher</code> that extends the <code>Person</code> class. I also have teaching sessions which  will store a list of students and a list of teachers that will be in that session.</p>

<pre><code>class Person {}
class Student extends Person {}
class Teacher extends Person {}

class Session {
  List&lt;Student&gt; students = new ArrayList();
  List&lt;Teacher&gt; teachers = new ArrayList();

  // add a person to the teaching session.
  // They should be placed into the appropriate lists
  public void enroll(Person p)
  {
    if (p instanceof Student)
      students.add((Student) p)
    else if (p instanceof Teacher)
      teachers.add((Teacher) p)
  }
}
</code></pre>

<p>The idea is that some other code will have a list of people and iterate the list to enroll them into the appropriate sessions as needed. However, the <code>enroll</code> method currently explicitly checks the object's type, which to me is undesirable and seems like bad design. </p>

<p>I have tried writing the <code>enroll</code> method like this using method overloading, which looks a lot cleaner</p>

<pre><code>public void enroll(Student p)
{
    students.add(p)
}

public void enroll(Teacher p)
{
    teachers.add(p)
}
</code></pre>

<p>but it seems like the code that iterates through the list of <code>Person</code> objects would need to then figure out whether the current person is a student or teacher instance, and type-cast appropriately before passing it to an <code>enroll</code> method.</p>

<p>Is there a way for me to design this so that I do not need to invoke <code>instanceof</code> at any point in time in my own code?</p>
