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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:19:30+00:00 2026-05-30T19:19:30+00:00

I think I’m asking about covariant return types. I have some generated code that

  • 0

I think I’m asking about covariant return types. I have some generated code that I’m trying to extend and use. Let’s suppose I have the following two classes:

public class SuperParent
{
    public List<SuperChild> getList()
    {
        return new ArrayList<SuperChild>();
    }
}
public class SuperChild
{
}

Now, I want to derive new classes from these thusly:

public class SubParent extends SuperParent
{
    public List<SubChild> getList()
    {
        return new ArrayList<SubChild>();
    }
}
public class SubChild extends SuperChild
{
}

The problem is, apparently I can’t override the getList() method because the return type doesn’t match, despite both classes being extended in the same direction. Can someone explain?

  • 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-30T19:19:32+00:00Added an answer on May 30, 2026 at 7:19 pm

    Your understanding of co-variant is correct but usasge is not. List<SubChild> is not the same as List<SuperChild>

    Consider this, List<Animals> is not the same as List<Dogs> and things can go horribly wrong if that was allowed. A Dog is an Animal but if it was allowed to assign like below:

    List<Dogs> dogs = new ArrayList<Dogs>();
    List<Animals> animals = dogs; //not allowed.
    

    then what happens when you add a cat to it?

    animals.add(new Cat());
    

    and

    Dog dog = dogs.get(0); //fail
    

    So its not allowed.

    As sugested by many others, use List<? extends SuperChild> as return type to solve your problem.

    EDIT
    To your comment above, if you do not have control over super class, i am afraid, you can not do anything.

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

Sidebar

Related Questions

think about having a skript where you have some words as input. now you
Think about the games like Spiral Knights and Minecraft(Which I believe both use LWJGL)
I think that asking this might be kind of silly but I'm still wondering
Think about the classic installation process, where you have a next button and when
Think that I have many activities,and all I want is this: I have a
I think that I have a quite strange question. I am using a class
Think that you have a content div whether the content of a website is
I think that I am misunderstanding something about how jquery $(this) works. In my
Think Design: I have many applications that share the same user database! Other tables
Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).

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.