title: Writing a constructor of a class that extends an ArrayList
body: <p>This is the signature of my class: </p>
  
  <pre class="lang-java prettyprint-override"><code>public class Constraint extends ArrayList&lt;Interval&gt; {
      // ...
  }
  </code></pre>
  
  <p>the other class Interval: </p>
  
  <pre><code>public class Interval {
      // ...
  }
  </code></pre>
  
  <p>has two <code>int</code>s, <code>first</code> and <code>last</code> </p>
  
  <p>Constructor:</p>
  
  <pre><code>public Interval(int first, int last) { 
      this.first = first;
      this.last = last;
  }
  </code></pre>
  
  <p>A method that returns the union of two Intervals or more but should be of Constraint type:</p>
  
  <pre><code> public Constraint union(Interval interval) {
       Interval a = new Interval(first, end);
       int l = 0;
       int max = 0;
       // Interval result = new Interval(l, max);
       l = (a.first &lt; interval.end) ? a.first : interval.end;
       max = (a.end &gt; interval.end) ? a.end : interval.end;
       return new Interval(l, max); 
       // the return here will return a new interval of type Interval but
       // the method that I'm suppose to write should return something of
       // type Constraint
  }
  </code></pre>
  
  <p>My main issue is: how can I write the following constructor?</p>
  
  <pre><code> public Constraint(Collection&lt;Interval&gt; collection) throws  NullPointerException {
      // if the collection is empty, I have to write something like this: 
      if (collection == null) {
          throw new NullPointerException("collection is empty");
      }
      // ...
  }
  </code></pre>
  
  <p>Any help on how I should write the constructor of the class Constraint is  really appreciated. </p>