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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:13:40+00:00 2026-05-26T17:13:40+00:00

I have a class TreeNode: public abstract class TreeNode<T>{ . . . public Collection<TreeNode<T>>

  • 0

I have a class TreeNode:

public abstract class TreeNode<T>{
   .
   .
   .

    public Collection<TreeNode<T>> children;

    public void clear(){
       if(children == null) 
      return;

       Iterator<TreeNode<T>> iterator = children.iterator();
       while(iterator.hasNext()){
          TreeNode<T> node = iterator.next();
          node.clear();
       }

       children.clear();
   }
   .
   .
   .

}

I then have a class ListTreeNode:

public class ListTreeNode<T> extends TreeNode<T>{
   .
   .
   .

   public ListTreeNode(T data, List<ListTreeNode<T>> children){
      this.data = data;
      this.root = null;
      this.children = children;
      this.childIndex = 0;
   }

   .
   .
   .

}

I get a compiler error saying that I cannot convert from List<ListTreeNode<T>> to Collection<TreeNode<T>>. Shouldn’t I be able to, since List is a subinterface of Collection and ListTreeNode is a subclass of TreeNode? Also, I have a corresponding class SetTreeNode which uses Set instead of List and there are no errors in its corresponding constructor where I have this.children = children; .

  • 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-26T17:13:41+00:00Added an answer on May 26, 2026 at 5:13 pm

    This problem has nothing to do with casting a List<T> to a Collection<T>, and everything to do with casting a List<SomeDerivedClass> to a List<SomeBaseClass>. Java generics are invariant, and the behavior you are looking for is covariance.

    This question has been raised many times on StackOverflow before. Here is one example: Is List<Dog> a subclass of List<Animal>? Why aren’t Java’s generics implicitly polymorphic?

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

Sidebar

Related Questions

I have a structure that looks following Class TreeNode { public TreeNode Parent {
I have a Tree. class TreeNode { public TreeNode(string name, string description) { Name
i already have a project with TreeNode class which creates a hierachy of nodes
Im having a problem with python.. I have a binary tree node type: class
I have class A: public class ClassA<T> Class B derives from A: public class
Lets say I have the following code: public class Base { // Some stuff
I have a class that inherits from TreeNode, called ExtendedTreeNode. To add an object
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have a node class I'm trying to use to populate a TreeView in
I have a generic BST and a dataItem class to act as treeNode's value.

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.