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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:44:30+00:00 2026-05-27T18:44:30+00:00

I’m having trouble understanding why the following doesn’t work and I’m sure the answer

  • 0

I’m having trouble understanding why the following doesn’t work and I’m sure the answer is related to something basic I am not understanding and hope someone can help.

I understand about using interfaces in an ArrayList such that if I have:

public interface Weapon { ... }
public class Gun implements Weapon { ...}
public class Knife implements Weapon { ... }

you can then insert anything that implements Weapon into the an array of weapons:

ArrayList<Weapon> weapons = new ArrayList<Weapon>();
weapons.add(new Gun());
weapons.add(new Knife();

I get that, but what is confusing me is the understanding of why ArrayList<Gun> isn’t compatible with ArrayList<Weapon> in other ways. To illustrate, the following is legal:

public void interfaceIsTheArgument(Weapon w) { ... }
...
interfaceIsTheArgument(new Gun());
interfaceIsTheArgument(new Knife());

but the following is not:

public void interfaceIsTheArgument(ArrayList<Weapon> w) { ... }
...
interfaceIsTheArgument(new ArrayList<Gun>());
interfaceIsTheArgument(new ArrayList<Knife>());

because the last function call reports that the method isn’t applicable for its arguments.

My question is why if the method knows it tasks an ArrayList with an interface as the generic type, why isn’t it okay to pass in an array list of knives in that last statement?

  • 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-27T18:44:31+00:00Added an answer on May 27, 2026 at 6:44 pm

    To “fix” the code, you need to use a generic bound:

    public void interfaceIsTheArgument(List<? extends Weapon> w) { ... }
    ...
    interfaceIsTheArgument(new ArrayList<? extends Weapon> ());
    interfaceIsTheArgument(new ArrayList<Knife>());
    

    The key reason is List<Gun> is not a subclass of List<Weapon>. The reason for this fact can be illustrated by this code:

    List<Gun> guns = new ArrayList<Gun>();
    // If List<Weapon> was a super type of List<Gun>, this next line would be allowed
    List<Weapon> weapons = guns; // Won't compile, but let's assume it did
    weapons.add(new Knife());  // Compiles, because Knife is a Weapon
    Gun gun = guns.get(0); // Oops! guns.get(0) is a Knife, not a Gun!
    

    By using the bound <? extends Weapon>, we are saying we’ll accept any generic type that is a subclass of Weapon. Using bounds can be very powerful. This kind of bound is an upper bound – we are specifying the top-level class as being Weapon.

    There’s also a lower bound, that uses this syntax:

    List<? super Weapon> // accept any type that is a Weapon or higher in the class hierarchy
    

    So, when to use each one? Remember this word PECS: “Producer extends, consumer super”. This means on the producer side of the code (where the objects are created) use extends, and on the consumer side of the code (where the objects are used) us super. Once you try it a few times, you’ll understand through experience why it works well.

    This SO question/answer covers it well.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.