Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - passing ArrayList of interface type

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?

like image 329
wirate Avatar asked May 23 '12 02:05

wirate


People also ask

Can you have a ArrayList of interface Java?

List is a Java interface that describes a sequential collection of objects. ArrayList is a class that describes an array-based implementation of the List Java interface. A new instance of the ArrayList class is obtained and assigned to List variable names .

Can we pass ArrayList as parameter in Java?

It is present in java. util package. If we want to pass an ArrayList as an argument to a function then we can easily do it using the syntax mentioned below. In the code above, we created an ArrayList object named 'list' and then we passed it to a function named modifyList.

Can ArrayList have different data types Java?

ArrayList cannot hold primitive data types such as int, double, char, and long. With the introduction to wrapped class in java that was created to hold primitive data values. Objects of these types hold one value of their corresponding primitive type(int, double, short, byte).


2 Answers

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) {
like image 131
Tim Pote Avatar answered Sep 21 '22 02:09

Tim Pote


Try ArrayList<? extends Damageable > _bodies.

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

like image 29
dann.dev Avatar answered Sep 24 '22 02:09

dann.dev