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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:00:29+00:00 2026-06-04T07:00:29+00:00

I have an interface Damageable as follows public interface Damageable { public void handleCollision(float

  • 0

I have an interface Damageable as follows

public interface Damageable {
    public void handleCollision(float impulse);
}

a class which implements this interface, BaseObject

public class BaseObject implements Damageable

Now in a third class, I have an ArrayList of the type BaseObject

public class ObjectManager {    
    public ArrayList<BaseObject> bodies;

What I am trying to do is to pass the ArrayList bodies to a method of another class which accepts ArrayList

public CollisionManager( ArrayList<Damageable> _bodies) {
        bodies = _bodies;
}

Java does not let me do new CollisionManager(bodies) where bodies is of type ArrayList and BaseObject implements Damageable

I have tried casting. Says cannot cast from
ArrayList<BaseObject> to ArrayList
Also tried using Class<? extends Damageable> but then I’m unable to call methods declared in the interface Damageable. How can I pass the ArrayList?

  • 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-04T07:00:30+00:00Added an answer on June 4, 2026 at 7:00 am

    You have to be explicit with your generics. Therefore, you have to inform the compiler that your generic type doesn’t have to be a Damagable per se, rather it can extend Damagable:

    public CollisionManager(ArrayList<? extends Damagable> bodies) {
    

    By the way, notice that I changed your variable to bodies rather than _bodies. Underscores are not part of the standard Java coding conventions.


    Edit in response to the OP’s comments

    Let’s say that, instead of an interface, you had a concrete class called Damagable. Telling the compiler <? extends Damagable> says that it doesn’t have to be an instance of Damagable. It’s okay that the type extend Damagable. Otherwise, the compiler assumes that you have a Damagable exactly.

    It doesn’t make as much sense when you think of Damagable as an interface, since there is not case where you would have an instance of Damagable. But they work in essentially the same way.

    You have to remember that you’re working with Java types, not classes. Java’s type syntax and structure is less robust than it’s class structure. There is no concept of implements when it comes to types.


    Last round of edits

    Finally, I should note that it’s generally better to use an interface for method/constructor parameters and method return types. This allows you and those that use your methods to use whatever implementation you please, and allows you to change your implementation as you please.

    So with those revisions, you would have:

    public CollisionManager(List<? extends Damagable> bodies) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have interface Foo public interface Foo { public void test(); } Class FooChild
Suppose I have: interface Foo { void doStuff(); } class FooImpl implements Foo {
i have interface: public interface Inx<T> { T Set(Data data); } simple class with
I have this interface: public interface Command<T> { T execute(String... args); } it works
I have seen that if I have interface named interfaceABC. Example: public class ABController
I have interface IDoc and an abstract class that implements it named Doc. I
I have interface with the event. My class implements the interface. The logic of
I have interface IA , interface IB extends IA and class A implements IA
I have interface IConfigurationSource { .... } and class FileConfigurationSource : IConfigurationSource { FileConfigurationSource(string
We have an interface which will be implemented by classes that take care of

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.