Multiple classes in an (undefined) ArrayList?

So a while ago I realised you could make ArrayLists with no class associated.

ArrayList sampleList = new ArrayList();

I am working on a simple 2d platformer at the moment and created a class that contains the gravity function and all the needed variables, which all the other gravity-affected objects could inherit from. I would like to put all those “physicsObjects” into an ArrayList (for example all the enemies and powerUps). The thing is that the instance of an Object class can not execute the functions of a player or enemy class. And if I created a playerClass object, it could not get any object from the sampleList.

void draw(){
  Object aPhysicalObject = new Object();
  playerClass aPlayer = new Player();
  for(int i = 0; i < sampleList.size(); i++){
    aPhysicalObject = sampleList.get(i); //works
    aPhysicalObject.display(); //doesn't work
    aPlayer = sampleList.get(i); //doesn't work
    aPlayer.display(); //works

If I could check which class the indexed-object had, maybe I could get it to work? I’m looking forward to your ideas! :smiley:

1 Like

This is the same as
ArrayList<Object> sampleList = new ArrayList<Object>();

Now an array list can hold objects of the <class> or any class that inherits from it. Since Object is the base class for all Java classes, including user defined classes your array list can hold objects of any type. The problem with this is that many poorly written programs crashed at runtime due to class casting exceptions.

This was the case before the Java developers introduced generics, now we type all our collections
ArrayList<MyUserClass> sampleList = new ArrayList<MyUserClass>();
The poorly written code is now detected at compile time so the running program is much more stable.


@quark Okay so I understood that you can add any subclass to an ArrayList, but how do I get the code from before to work? Is that even possible? :sweat_smile:

Try this

aPlayer = (playerClass) sampleList.get(i);

What was the error message?

1 Like

So I don’t have much time right now but I put that line in and it didn’t give me an error message. I’ll have to adjust my code later to see if it really works! One question though: What exactly does that do? Is it like “if sampleList(i) == playerClass get it else do nothing” or what?

In your code

  • aPlayer if of type playerClass
  • sampleList holds instances of type Object
  • sampleList.get(i); returns an instance of type `Object’

A variable of type playerClass can not reference an instance of type Object so
aPlayer = sampleList.get(i);

In this code
aPlayer = (playerClass) sampleList.get(i);
the returned Object instance is cast to the type playerClass so it’s reference can be stored in a playerClass variable i.e. aPlayer hence it works. The technique is called casting

The problem with this code is, if the returned instance is not of type playerClass (or a child class of playerClass) then the statement
aPlayer = (playerClass) sampleList.get(i);
will crash at runtime.

That is why you never see lists declared like this
ArrayList sampleList = new ArrayList();
except in ancient legacy code.


Okay, thank you! :smiley:

Depending on what you want to do with the objects in the list, you can either inherit all your gravity objects from a common gravityObject class and cast to this when accessing members of the list. Or you create a common gravity object interface which exposes the gravity methods for all objects in the list. You can cast to an interface type just like you cast to any parent object type.

What’s an interface? :sweat_smile:

It’s like a contract a class must fulfill. If a class implements an interface, it must provide all the fields and methods defined in the interface. Make a quick search about “java interface” and you will find a lot resources. Happy coding :slight_smile:

Not the fields. Actually, any fields declared in an interface are implicitly public, static & final! :no_mouth:

1 Like

6 posts were split to a new topic: Redundant declarations in interface PConstants