# Using inheritance with ArrayList

**URL:** <https://discourse.processing.org/t/using-inheritance-with-arraylist/4246>\
**Category:** Coding Questions\
**Created:** [October 8, 2018, 11:18am UTC](https://discourse.processing.org/t/using-inheritance-with-arraylist/4246 "2018-10-08T11:18:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [October 8, 2018, 11:18am UTC](https://discourse.processing.org/t/using-inheritance-with-arraylist/4246/1 "2018-10-08T11:18:23Z")

</div>

Hi,

I’ve been using inheritance for some of my classes, but I’ve run into some troubles when trying to do the same things with `ArrayList`'s.

If I have 2 classes `class ObjectType1 { }`, `class ObjectType2 extends ObjectType1 { }`, and have a function `void test(ObjectType1 object) { }`, the function `test` can take in `ObjectType1` or `ObjectType2`. For example:

```auto
class ObjectType1 {
  ObjectType1() {
  }
}

class ObjectType2 extends ObjectType1 {
  ObjectType2() {
  }
}

ObjectType1 object1 = new ObjectType1();
ObjectType2 object2 = new ObjectType2();

void test(ObjectType1 object) {
}

void draw() {
  test(object1);
  test(object2);
}

```

Would work fine, but when I switch everything out to `ArrayList`'s:

```auto
class ObjectType1 {
  ObjectType1() {
  }
}

class ObjectType2 extends ObjectType1 {
  ObjectType2() {
  }
}

ArrayList<ObjectType1> object1 = new ArrayList<ObjectType1>();
ArrayList<ObjectType2> object2 = new ArrayList<ObjectType2>();

void test(ArrayList<ObjectType1> object) {
}

void draw() {
  test(object1);
  test(object2);
}

```

Would return an error. I could just loop through all of the `ArrayList`'s elements, but it would be helpful to know if there was a way to have a function be able to take `ArrayList<ObjectType1>` and `ArrayList<ObjectType2>`.

Any questions or advice are appreciated,  
Thanks!

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 8, 2018, 11:23am UTC](https://discourse.processing.org/t/using-inheritance-with-arraylist/4246/2 "2018-10-08T11:23:46Z")

</div>

> [@Sarah](#):
>
> void test(ArrayList\<ObjectType1\> object) { }

Try

```auto
void test(ArrayList<? extends ObjectType1> object) {

```

and welcome to the weird world of Java generics! 😜

Inheritance doesn’t work the same way with generics - see [Generics, Inheritance, and Subtypes (The Java™ Tutorials \> Learning the Java Language \> Generics (Updated))](https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html)

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [October 8, 2018, 12:20pm UTC](https://discourse.processing.org/t/using-inheritance-with-arraylist/4246/3 "2018-10-08T12:20:58Z")

</div>

It works, thanks so much! Looks like I have a lot to learn! 🙂

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 8, 2018, 1:37pm UTC](https://discourse.processing.org/t/using-inheritance-with-arraylist/4246/4 "2018-10-08T13:37:30Z")

</div>

> [@Sarah](#):
>
> Looks like I have a lot to learn!

I’ve been coding Java over 15 years now and I still have to look up how generics work half the time! 😄
