20

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

3 Answers 3

44

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) {
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the reply, but the ArrayList HAS to implement Damageable since I am using methods of that interface.
? extends Damagable means implements Damagable in Java generic-speak.
let me confirm it. ArrayList<? extends Damageable> means an ArrayList of any type that extends or implements the interface Damageable right?
Then how is that different from ArrayList<Damageable>? Does Java not know that I am passing an interface and that a concrete type behind it is needed?
Although a little scope creep, I wish this discussion also illustrated how to iterate the List in a for loop if all you know is the interface name. Hint: it's not for( ? extends Damageable x : myList )
|
2

Try ArrayList<? extends Damageable > _bodies.

This says that you want an ArrayList consisting of a Class that extends (well implements) Damageable

4 Comments

What is the difference between ArrayList< <Class <? extends Damageable> > and ArrayList<? extends Damageable >?
I may be wrong here, but I think nothing. The one you have put specifies that it must be a Class, and extend/implement Damageable, but of course it has to be a class anyway (again I think). This is approaching the limits of my Generics knowledge though. Does your one compile?
ArrayList< <Class <? extends Damageable> > wasn't workable because doing this wont let me call methods that have been declared in the interface.
@wirate I think what you're thinking is correct in your first comment on this answer, however, what you typed won't compile. ArrayList<Class <? extends Damageable>> (note the number of opening angle brackets) will compile, but then you'll have a list of Class objects rather than a list of Damagable objects.
2

Others will no doubt point out the technical solutions from the Java syntax perspective.

I'm just going to mention a few design issues that you should perhaps consider:

  • Your BaseObject implements Damageable. That means all BaseObjects are Damageable. Why not then just make a CollisionManager(ArrayList<BaseObject>)? Unless you are going to use Damageable elsewhere (i.e. there are things which are Damageable but are not BaseObjects) then it seems like an unnecessary abstraction.
  • Usually for collision detection in a game / simulation you would want to use a spatial data structure for collision detection (e.g. Octree, Quadtree or AABB tree) rather than an ArrayList. Searching for collisions in an ArrayList is an O(n^2) algorithm. This will become a big issue if you have a lot of live objects.
  • Damageable seems like a bad name, since the functionality relates to collision detection rather than damage - wouldn't Collidable be better?

1 Comment

In fact I did what you suggested in point 1. I was just being cautious. Point 2: Box2D is detecting collisions and I just have to call a method on this type when a collision is detected Point 3: There are objects in my situation that collide but are never damaged

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.