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 6194229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:15:39+00:00 2026-05-24T03:15:39+00:00

Ok so I have a Node in a tree which potentially has a parent

  • 0

Ok so I have a Node in a tree which potentially has a parent (also a Node) and some children (also Nodes).

What I wanted to do with Java’s generics is to allow passing in the type of List the child Nodes would be stored in as well as the data the Node would hold. So I wrote the following class which the compiler seemed happy with.

public class Node<T extends List<Node<T, U>>, U>
{
    public Node<T, U> parent;
    public T children;
    public U data;

    private Class<T> tClass;

    public Node(Class<T> tClass) throws InstantiationException, IllegalAccessException
    {
        this.tClass = tClass;
        this.children = this.tClass.newInstance();
    }
}

The problem was when I tried to initialise a Node.

Node<ArrayList, NodeData> node = new Node(ArrayList.class);

The compiler complains because of a Bound mismatch on the ArrayList. I tried to fix it with the following attempts:

Node<ArrayList<Node>, NodeData> node = new Node(ArrayList.class);
Node<ArrayList<Node<ArrayList, NodeData>>, NodeData> node = new Node(ArrayList.class);
Node<ArrayList<Node<ArrayList<Node>, NodeData>>, NodeData> node = new Node(ArrayList.class);
Node<ArrayList<Node<ArrayList<Node<ArrayList, NodeData>, NodeData>>, NodeData> node = new Node(ArrayList.class);

As you can see this will go on forever, I know I could fix this with

public class Node<T extends List<Node>, U>
{
    public Node<T, U> parent;
    public T children;
    public U data;

    private Class<T> tClass;

    public Node(Class<T> tClass) throws InstantiationException, IllegalAccessException
    {
        this.tClass = tClass;
        this.children = this.tClass.newInstance();
    }
}

Which allows me to use:

Node<ArrayList<Node>, NodeData> node = new Node(ArrayList.class);

However when I do something like:

node.children.get(0).children;

The returned type is a List, not an ArrayList which is what I wanted.

Is what I’m trying to do possible? If so perhaps someone could show me where I’m going wrong or if not what’s the best alternative?

Thanks, Richy.

  • 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-24T03:15:39+00:00Added an answer on May 24, 2026 at 3:15 am

    As you discovered, recursive generics become very complicated very quickly. I see two options for you.

    1: Remove T and let children be declared a List<Node<U>>

    2: Declared Node to be abstract, add a self reference, and define a concrete ArrayList node reference.

    abstract class Node<N extends  Node<N, T, U>, T extends List<N>, U>
    {
        public Node<N, T, U> parent;
        public T children;
        public U data;
    
        private Class<T> tClass;
    
        public Node(Class<T> tClass) throws InstantiationException, IllegalAccessException
        {
            this.tClass = tClass;
            this.children = this.tClass.newInstance();
        }
        {
            Node<ALNode<Integer>, ArrayList<ALNode<Integer>>, Integer> node = new ALNode<Integer>();
            ALNode<Integer> node2 = new ALNode<Integer>();
        }
    }
    
    class ALNode<U> extends Node<ALNode<U>, ArrayList<ALNode<U>>, U> {
    
        public ALNode() throws InstantiationException,
                IllegalAccessException {
            super((Class) ArrayList.class);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Java Tree in which tree nodes could have children that take
I have a tree structure where each Node has a parent and a Set<Node>
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have a tree data structure that is L levels deep each node has
Which Tree data structure in Java allows querying for different levels of children? I
I have ultraTree (infragistics tree) which is created at design time and it has
I have a n-ary tree which contains key values (integers) in each node. I
I have a JSON tree that contains nodes and children - the format is:
Im having a problem with python.. I have a binary tree node type: class
I have a binary tree where each node can have a value. I want

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.