title: Can a method be able to return different data types?
body: <p>I have a class named <code>matFactory()</code>. Inside it, there is a method named <code>lastAddedObj(..)</code>.</p>
  
  <p>The latter method receives an <code>ArrayList</code> as a parameter and returns the last added object in this list.</p>
  
  <p>My problem is: I have two kind of lists, <code>ArrayList&lt;Mat&gt;</code> and <code>ArrayList&lt;MatOfKeyPoint&gt;</code> and I want the method <code>lastAddedObject</code> to be applicable for both types. As you see the method signature below:</p>
  
  <pre><code>public Mat lastAddedObj(ArrayList&lt;Mat&gt; list) {
  ....
  ....
      return list.get(size -1);
  }
  </code></pre>
  
  <p>And I want this method to be able for both <code>ArrayList&lt;Mat&gt;</code> and <code>ArrayList&lt;MatOfKeyPoint&gt;</code>, because as you see above, if I passed <code>ArrayList&lt;Mat&gt;</code> it will return <code>Mat</code> object and if will pass <code>ArrayList&lt;MatOfKeyPoint&gt;</code> it will return <code>MatOfKeyPoint</code> object.</p>
  
  <p>How to solve this issue?</p>