title: How do I declare a variable that contains a subclass of a class which implements an interface?
body: <p>I want to declare a variable which holds a class which implements a specific interface.  Specifically, I'm trying to store a <code>SocketChannel</code> and a <code>DatagramChannel</code> in the same property so I can use them interchangeably.  Both of these classes extend <code>SelectableChannel</code> and also implement <code>ByteChannel</code>, and I wish to call methods from both.  I do not want to store this in two separate variables, because I they need to be the same object.  I want to pass this object in one variable to the constructor.</p>
  
  <p>Is it even possible to do this?  If not, what are the common workarounds which still support this type of model?  For clarity, here are the (incorrect) declarations which could describe what I'm trying to do, were they valid:</p>
  
  <pre>
  private SelectableChannel & ByteChannel byteChannel;
  private SelectableChannel implements ByteChannel byteChannel;
  private ? extends SelectableChannel implements ByteChannel byteChannel;
  </pre>
  
  <p><strong>Please Note:</strong></p>
  
  <p>I am not looking for other ways to handle networking which avoids this problem, or other ways to implement networking.  This question is about declaring a variable to hold a subclass of a class which also implements a specific interface.  I only gave the specifics so that you would know that I cannot create a new interface or subclass because in this case all of the classes involved are part of the <code>java.nio</code> package.</p>