Search code examples
javavariablesinterfacesubclassnio

How do I declare a variable that contains a subclass of a class which implements an interface?


I want to declare a variable which holds a class which implements a specific interface. Specifically, I'm trying to store a SocketChannel and a DatagramChannel in the same property so I can use them interchangeably. Both of these classes extend SelectableChannel and also implement ByteChannel, 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.

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:

private SelectableChannel & ByteChannel byteChannel;
private SelectableChannel implements ByteChannel byteChannel;
private ? extends SelectableChannel implements ByteChannel byteChannel;

Please Note:

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 java.nio package.


Solution

  • There is no way in Java to declare the variable the way you would like to do it.

    You could use SelectableChannel for the type of the variable (since this is a supertype of both SocketChannel and DatagramChannel), and cast it to a ByteChannel whenever you need to call methods from that interface. Simple example:

    class MyClass {
        private SelectableChannel channel; // either a SocketChannel or a DatagramChannel
    
        public int readStuff(ByteBuffer buffer) {
            // Cast it to a ByteChannel when necessary
            return ((ByteChannel) channel).read(buffer);
        }
    }
    

    (Or the other way around: declare the variable as a ByteChannel and cast to a SelectableChannel when necessary - whichever is more convenient in your case).