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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:14:07+00:00 2026-05-11T18:14:07+00:00

Well, I have a class Customer (no base class). I need to cast from

  • 0

Well, I have a class Customer (no base class).

I need to cast from LinkedList to List. Is there any clean way to do this?

Just so you know, I need to cast it to List. No other type will do. (I’m developing a test fixture using Slim and FitNesse).


EDIT: Okay, I think I need to give code examples here.

import java.util.*;
public class CustomerCollection
{
    protected LinkedList<Customer> theList;

    public CustomerCollection()
    {
        theList = new LinkedList<Customer>();
    }

    public void addCustomer(Customer c){ theList.add(c); }
    public List<Object> getList()
    {
        return (List<? extends Object>) theList;
    }
}

So in accordance with Yuval A’s remarks, I’ve finally written the code this way. But I get this error:

CustomerCollection.java:31: incompatible types
found   : java.util.List<capture#824 of ? extends java.lang.Object>
required: java.util.List<java.lang.Object>
        return (List<? extends Object>)theList;
               ^
1 error

So, what’s the correct way to do this cast?

  • 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-11T18:14:07+00:00Added an answer on May 11, 2026 at 6:14 pm

    You do not need to cast. LinkedList implements List so you have no casting to do here.

    Even when you want to down-cast to a List of Objects you can do it with generics like in the following code:

    LinkedList<E> ll = someList;
    List<? extends Object> l = ll; // perfectly fine, no casting needed
    

    Now, after your edit I understand what you are trying to do, and it is something that is not possible, without creating a new List like so:

    LinkedList<E> ll = someList;
    List<Object> l = new LinkedList<Object>();
    for (E e : ll) {
        l.add((Object) e); // need to cast each object specifically
    }
    

    and I’ll explain why this is not possible otherwise. Consider this:

    LinkedList<String> ll = new LinkedList<String>();
    List<Object> l = ll; // ERROR, but suppose this was possible
    l.add((Object) new Integer(5)); // now what? How is an int a String???
    

    For more info, see the Sun Java generics tutorial. Hope this clarifies.

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

Sidebar

Ask A Question

Stats

  • Questions 270k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MooTools has a $type(), where you pass in an object.… May 13, 2026 at 1:34 pm
  • Editorial Team
    Editorial Team added an answer We do this by having a single tableview, and then… May 13, 2026 at 1:34 pm
  • Editorial Team
    Editorial Team added an answer Well it has been a long time since I've asked… May 13, 2026 at 1:34 pm

Related Questions

In one of my projects, I have an application that manages several clients (or
So, let's say I have this code (VB.Net): Sub Main() dim xxx as string
I have a library Eclipse project/workspace containing functionality that is occasionally delivered to a
My question (which will follow after this, sorry about the long intro, the question

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.