# Multiple classes in an (undefined) ArrayList?

**URL:** <https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291>\
**Category:** Beginners\
**Created:** [February 11, 2019, 7:14pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291 "2019-02-11T19:14:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [February 11, 2019, 7:14pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/1 "2019-02-11T19:14:45Z")

</div>

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

```auto
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.

```auto
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! 😃

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 11, 2019, 7:44pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/2 "2019-02-11T19:44:12Z")

</div>

> [@Schred](#):
>
> ArrayList sampleList = new ArrayList();

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.

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [February 11, 2019, 7:51pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/3 "2019-02-11T19:51:01Z")

</div>

@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? 😅

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 11, 2019, 8:00pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/4 "2019-02-11T20:00:43Z")

</div>

Try this

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

```

What was the error message?

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [February 12, 2019, 1:30pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/5 "2019-02-12T13:30:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 12, 2019, 2:07pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/6 "2019-02-12T14:07:11Z")

</div>

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);`  
fails

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.

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [February 12, 2019, 5:46pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/7 "2019-02-12T17:46:19Z")

</div>

Okay, thank you! 😃

---

<div class="post-metadata">

**Author:** ![needfulthing](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@needfulthing](https://discourse.processing.org/u/needfulthing)\
**Post date:** [February 20, 2019, 11:49am UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/8 "2019-02-20T11:49:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [February 21, 2019, 4:54pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/9 "2019-02-21T16:54:30Z")

</div>

What’s an interface? 😅

---

<div class="post-metadata">

**Author:** ![needfulthing](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@needfulthing](https://discourse.processing.org/u/needfulthing)\
**Post date:** [February 21, 2019, 5:09pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/10 "2019-02-21T17:09:36Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 21, 2019, 5:28pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/11 "2019-02-21T17:28:56Z")

</div>

> [@needfulthing](#):
>
> If a class implements an `interface`, it must provide all the fields and …

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

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 21, 2019, 7:00pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/12 "2019-02-21T19:00:30Z")

</div>

6 posts were split to a new topic: [Redundant declarations in interface PConstants](https://discourse.processing.org/t/redundant-declarations-in-interface-pconstants/8602)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 4, 2023, 10:43am UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/13 "2023-04-04T10:43:12Z")

</div>

@quark :

> [@quark](#):
>
> the returned `Object` instance is _cast_ to the type `playerClass`

Can I ask the object which type it is?

```auto

    println(someObject.getType());

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 4, 2023, 10:58am UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/14 "2023-04-04T10:58:17Z")

</div>

You can get any object to report the class it was instantiated from

```auto
// Object hierarchy
class A{ }
class B extends A { }
class C extends B { }

// Declare 3 variables of type A
A a,b,c;

// Now instantiate an object from each class
a = new A();
b = new B();
c = new C();

// Get each object to report its class name
println(a.getClass().getSimpleName());
println(b.getClass().getSimpleName());
println(c.getClass().getSimpleName());

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 4, 2023, 10:59am UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/15 "2023-04-04T10:59:21Z")

</div>

this seems to work:

```auto

// imports ----

import java.lang.reflect.Field;
import java.util.Collections;

....
...
....

  println("---------------------------------------------------------");

  println("\n *****METHODS*****");
  for (java.lang.reflect.Method c : getClass().getDeclaredMethods()) {
    //
    println("-----------------------------");

    println(c.getClass());

    println((String) c.getName());
  }//for

```

cf. [Object (Java Platform SE 7 )](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html)  
cf. [Class (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html)  
cf. [Method (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 4, 2023, 11:02am UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/16 "2023-04-04T11:02:42Z")

</div>

```auto
    println(c.getClass());

```

Can I ask the Method c which methods it is using (which OTHER methods it is calling) ?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 4, 2023, 11:09am UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/17 "2023-04-04T11:09:54Z")

</div>

> [@Chrisir](#):
>
> Can I ask the Method c which methods it is using (which OTHER methods it is calling) ?

Interesting question but AFAIK there is no simple way to do this.

The source code in the `c` would be compiled to Java byte code before execution and I don’t think the compilation process preserves the variable and method names from the source code by default.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 4, 2023, 11:13am UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/18 "2023-04-04T11:13:41Z")

</div>

Just a thought 🤔

If a sketch crashes with an un-handled exception it produces a stack trace which lists the methods called preceding the exception so the JVM knows what methods have been executed but that still doesn’t mean it knows what method calls are coming up.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 4, 2023, 11:15am UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/19 "2023-04-04T11:15:15Z")

</div>

I see that an Object has Member: [Method (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html)

How can I get the members or a member Object from my method?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 4, 2023, 12:01pm UTC](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291/20 "2023-04-04T12:01:14Z")

</div>

@quark

**Question One**

Cool would be when a method could tell us it’s name

```auto
  println("I am "
    + this.getClass().getName());

```

but that’s only the name of the Sketch!

How can I get the name of the method I am in?

* * *

**Question Two**

And can i retrieve the full text of a method:

```auto
  for (java.lang.reflect.Method currentMethod : getClass().getDeclaredMethods()) {

    print("toString() gives : ");
    println(currentMethod.toString()); // works, gives only header

    println(currentMethod.getBody()); // ????????????????????
}

```

[Next page](https://discourse.processing.org/t/multiple-classes-in-an-undefined-arraylist/8291.md?page=2)
