# Unknown class through a function

**URL:** <https://discourse.processing.org/t/unknown-class-through-a-function/12236>\
**Category:** Coding Questions\
**Created:** [June 21, 2019, 5:33pm UTC](https://discourse.processing.org/t/unknown-class-through-a-function/12236 "2019-06-21T17:33:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FlyingApple](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@FlyingApple](https://discourse.processing.org/u/FlyingApple)\
**Post date:** [June 21, 2019, 5:33pm UTC](https://discourse.processing.org/t/unknown-class-through-a-function/12236/1 "2019-06-21T17:33:03Z")

</div>

```auto
void collision(Player player) {
  for (Wall wall : walls) {
     //find collision between player and wall, if they collide, stop player movement

```

Is it possible for the example above to have an unknown class passed through the function? For example i could pass through player or enemy and they both work in the same way.

---

<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:** [June 21, 2019, 6:17pm UTC](https://discourse.processing.org/t/unknown-class-through-a-function/12236/2 "2019-06-21T18:17:28Z")

</div>

Create a shared parent class for those subclasses to inherit from. 💡

---

<div class="post-metadata">

**Author:** ![macbrayne](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@macbrayne](https://discourse.processing.org/u/macbrayne)\
**Post date:** [June 23, 2019, 9:40pm UTC](https://discourse.processing.org/t/unknown-class-through-a-function/12236/3 "2019-06-23T21:40:03Z")

</div>

A very extensible method is using interfaces: All classes relevant to collision detection implement the Collidable but each class has to implement the methods themselves. Thus there are no inheritance problems. You can also check whether Enemies are colliding with other objects and so on:

```auto
ArrayList<Collidable> collidables = new ArrayList<Collidable>();
interface Collidable {
  float getX();
  float getY();
  // Etc. All you need for colliding
  void onCollide(); // Gets called on collision
}
class Player implements Collidable {
  // Implement interface methods and do other stuff
}
void collide(Collidable c) {
  for(Collidable collidable : collidables) {
    // If it collides run c.onCollide()! 
  } 
}

```

P. S. Writing this on a Smartphone. Syntax errors may or may not arise.
