Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3284946
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:12:42+00:00 2026-05-17T20:12:42+00:00

I want to declare a variable which holds a class which implements a specific

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:12:43+00:00Added an answer on May 17, 2026 at 8:12 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract base class and I want to declare a field or
I want to declare two beans and instantiate them using Spring dependency injection? <bean
I want to use CodeDOM to both declare and initialize my static field in
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my
I want to loop over the contents of a text file and do a
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I want to reference a COM DLL in a .NET project, but I also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.