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

  • Home
  • SEARCH
  • 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 737263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:42:05+00:00 2026-05-14T07:42:05+00:00

I am writing an application to do some distributed calculations in a peer to

  • 0

I am writing an application to do some distributed calculations in a peer to peer network. In defining the network I have two class the P2PNetwork and P2PClient. I want these to be generic and so have the definitions of:

P2PNetwork<T extends P2PClient<? extends P2PNetwork<T>>>

P2PClient<T extends P2PNetwork<? extends T>>

with P2PClient defining a method of setNetwork(T network). What I am hoping to describe with this code is:

  1. A P2PNetwork is constituted of
    clients of a certain type
  2. A P2PClient may only belong to a
    network whose clients consist of the
    same type as this client (the
    circular-reference)

This seems correct to me but if I try to create a non-generic version such as

MyP2PClient<MyP2PNetwork<? extends MyP2PClient>> myClient;

and other variants I receive numerous errors from the compiler. So my questions are as follows:

  1. Is a generic circular reference even
    possible (I have never seen anything explicitly forbidding it)?
  2. Is the above generic definition a
    correct definition of such a circular
    relationship?
  3. If it is valid, is it the “correct”
    way to describe such a relationship
    (i.e. is there another valid
    definition, which is stylistically
    preferred)?
  4. How would I properly define a
    non-generic instance of a Client and
    Server given the proper generic
    definition?
  • 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-14T07:42:05+00:00Added an answer on May 14, 2026 at 7:42 am

    Circular generic references are indeed possible. Java Generics and Collections includes several examples. For your case, such a specimen would look like this:

    public interface P2PNetwork<N extends P2PNetwork<N, C>,
                                C extends P2PClient<N, C>> {
      void addClient(C client);
    }
    
    public interface P2PClient<N extends P2PNetwork<N, C>,
                                C extends P2PClient<N, C>> {
      void setNetwork(N network);
    }
    
    class TorrentNetwork implements P2PNetwork<TorrentNetwork, TorrentClient> {
      @Override
      public void addClient(TorrentClient client) {
        ...
      }
    }
    
    class TorrentClient implements P2PClient<TorrentNetwork, TorrentClient> {
      @Override
      public void setNetwork(TorrentNetwork network) {
        ...
      }
    }
    
    ...
    
    TorrentNetwork network = new TorrentNetwork();
    TorrentClient client = new TorrentClient();
    
    network.addClient(client);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an application in BlackBerry, where I want to do some custom
I am writing a distributed application in Scala that uses Akka actors. I have
I am writing an application for Windows Mobile where I want to display some
I'm writing some sms application for android and i have a problem. I send
I'm writing an application that manipulates some sort of social network data, so the
I'm writing an application which need to have some things done by the matlab
I’m writing Qt application using some 3rd party library. I want to package it
I'm writing an application to send some images to a third party, and the
I am writing for my desktop some application for handling some services. I wrote
i'm writing an application that need to add some events to a calendar in

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.