title: Is it possible to extend byte array?
body: <p>I am implementing AES for an assignment, and would like to encapsulate the logic of checking that a key is always an array of bytes whose length is exactly 16, 24 or 32.</p>
  
  <p>I can create a wrapper class that stores the bytes as a byte[] internally, but I wonder if it is possible to extend Array or something similar so that I don't have to reimplement Iterators, toString and other convenience methods that I get from using Java array.</p>
  
  <p>I.e. is something like the following possible? </p>
  
  <pre><code>public class Key extends Array&lt;Byte&gt;{
  
      // Valid length of key in bytes
      public static int[] possibleLengths = new int[]{ 16, 24, 32 };
  
      // Check if a given byte[] is a valid candidate for a key
      public static boolean isValidLength(byte[] input) {
          for (int length: possibleLengths) {
              if (input.length == length) return true;
          }
          return false;
      }
  
      // Primary constructor passes bytes directly to internal state
      public Key(byte[] input) throws InvalidKeyLengthException {
          if (!isValidLength(input))
              throw new InvalidKeyLengthException;
  
          setKey(input);
      }
  }
  </code></pre>