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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:50:58+00:00 2026-06-08T08:50:58+00:00

So I have two classes. Class B extends class A . I’m creating an

  • 0

So I have two classes. Class B extends class A. I’m creating an ArrayList containing objects of type B, so the list is of type ArrayList<B>. I want to use that ArrayList in a method useList that has an input parameter type of ArrayList<A>. It stands to reason that since B is a child class of A, it can be used everywhere that A is, including in this application.

HOWEVER, the compiler doesn’t seem to like this (see error messages in code below). I also tried casting my ArrayList<B> to ArrayList<A> when passing it into the method, but that one didn’t work either.

Code:

public class Test {
    private class A { }
    private class B extends A { }
    public static void useList(ArrayList<A> aList) { }

    public static void main(String[] args) {

        ArrayList<B> bList = new ArrayList<B>();

        /* "The method test(ArrayList<Test.A>) in the type Test
         * is not applicable for the arguments (ArrayList<Test.B>)" */
        useList(bList); 

        /* "Cannot cast from ArrayList<Test.B> to ArrayList<Test.A>" */
        useList((ArrayList<A>)bList);
    }
}

I’m undoubtedly doing something silly, so any advice would be appreciated!

Thanks!

  • 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-08T08:51:02+00:00Added an answer on June 8, 2026 at 8:51 am

    just declare useList to expect a parameter of type ArrayList with elements that extend A:

    public static void useList(ArrayList<? extends A> aList) { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes Foo and Bar that Bar extends Foo as below: class
suppose I have two classes class A extends class B class A has its
Say I have two classes which extend Event : public class CustomEventOne extends Event
I have two classes Parent and Child . class Child extends Parent { private
I have two classes that extend the same abstract class. They both need the
I have two Java classes: B, which extends another class A, as follows :
I have two classes; my Main class and a class called BlockPlace. I want
I have two classes: package a; class A { private <type> fieldOfClassA; // getters,
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend
I have two classes class A { public string something { get; set; }

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.