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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:47:42+00:00 2026-06-08T18:47:42+00:00

I had an error while generalizing some classes in order to reduce code duplicates

  • 0

I had an error while generalizing some classes in order to reduce code duplicates in my project, the case is as follows:

interface ClassRoot{}

public class ClassChild1 implements ClassRoot{
    // constructor is omitted for simplicity
    public Collection<ClassChild1> createList(){

        Collection<ClassChild1> col = getCollectionFromSomewhere();
        return col;
    }
}

public class Class1{
    protected <T extends ClassRoot> Collection<T> doSth(){
        Collection<T> myCol = null;
        ClassChild1 child = new ClassChild1();

        // here I get compile time assignment error
        // also (assuming that myCol not null) myCol.add(new ClassChild1());
        // is not permitted.
        myCol = child.createList();
        return myCol;
    }
}

Isn’t that against the polymorphism phenomenon? I know that for example,

List<Object> list1;

can never be (and should not be assigned for typesafety) to:

List<String> list2;

But here my case is different, i specified the Type Parameter extending some specific class expecting to make use of OOP Polymorphism concept, and doing assignments right exactly implementing right class.
Now I have 2 questions;

  • May someone clearly explain why is that is forbidden in Java, for what justifiable reason?
  • How can I implement such a functionality, by using type params or wildcards?
  • 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-06-08T18:47:44+00:00Added an answer on June 8, 2026 at 6:47 pm

    The assignment

    Collection<T> myCol = null;
    myCol = child.createList();
    

    will fail because you are assigning Collection<ClassChild1> to a Collection parameterized with an unknown type T, which may or may not be ClassChild1.

    You have specified T as inferred at the call site, so imagine the following code:

    Class1 c1;
    Collection<ClassChild2> c = c1.doSth();
    

    You can see that the compiler would paint itself into a corner if it allowed you to compile such a doSth method since inside that method you want to add a ClassChild1 instance to a collection that in this example happens to hold instances of ClassChild2.

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

Sidebar

Related Questions

I just had this throw a compilation error while refactoring some legacy (hence VB.NET)
In my code I had an error that was catched by following exceptions while
I moved my project from windows to ubuntu and had this error while trying
I got an error today while trying to do some formatting to existing code.
I had an access to modified closure error in the code below foreach (var
One of my users had a single error while opening a file (I'm using
I've met an unknown error while inserting data into the database. The LogCat had
I get this error while starting jboss, has anyone had it before? 17:10:14,352 ERROR
I had never had this error before, and I didn't move any file, or
I had this error when I browse new web site that site sub from

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.