title: Method to accept one class object or another class object
body: <p>I have two methods like the following</p>
  
  <pre><code>private List&lt;Long&gt; getIds(String name, List&lt;Cat&gt; cats) {
      List&lt;Long&gt; catIds = new ArrayList&lt;&gt;();
      for (Cat cat : cats) {
          if (cat.getName().equals(name)) catIds.add(cat.getId());
      }
      return catIds;
  }
  
  private List&lt;Long&gt; getIds(String name, List&lt;Dog&gt; dogs) {
      List&lt;Long&gt; dogIds = new ArrayList&lt;&gt;();
      for (Dog dog : dogs) {
          if (dog.getName().equals(name)) dogIds.add(dog.getId());
      }
      return dogIds;
  }
  </code></pre>
  
  <p>My cat and dog class are as follows</p>
  
  <pre><code>public class Cat {
      String name;
      Long id;
      // additional variables
      // getters and setters
  }
  
  public class Dog {
      String name;
      Long id;
      // additional variables
      // getters and setters
  }
  </code></pre>
  
  <p>Just to avoid redundancy, I wanted to convert the above two methods to a single generic method.</p>
  
  <p>I tried the following</p>
  
  <pre><code>private List&lt;Long&gt; getIds(String name, List&lt;T&gt; objects) {
      List&lt;Long&gt; ids = new ArrayList&lt;&gt;();
      for (T object : objects) {
          if (object.getName().equals(name)) ids.add(object.getId());
      }
      return ids;
  }
  </code></pre>
  
  <p>But it does not work out as it complains that the generic T does not have <code>getName</code> or <code>getId</code></p>
  
  <p>Here <code>Cat</code> and <code>Dog</code> are in-built java classes. As a result I <strong>CANNOT</strong> perform inheritance and provide a super class <code>Animal</code> for them with <code>name</code> and <code>id</code> as data variables.</p>
  
  <p>Is there any way I could accomplish merging the two above methods without implementing inheritance?</p>