# Accessing variables in an array of different objects

**URL:** <https://discourse.processing.org/t/accessing-variables-in-an-array-of-different-objects/17324>\
**Category:** Beginners\
**Created:** [January 25, 2020, 10:51am UTC](https://discourse.processing.org/t/accessing-variables-in-an-array-of-different-objects/17324 "2020-01-25T10:51:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![J0R1AN](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/j0r1an/32/7498_2.png) [@J0R1AN](https://discourse.processing.org/u/J0R1AN)\
**Post date:** [January 25, 2020, 10:51am UTC](https://discourse.processing.org/t/accessing-variables-in-an-array-of-different-objects/17324/1 "2020-01-25T10:51:45Z")

</div>

I am working on a bigger project with a game that has an AI and Human player. I want to expand it so I have an array of players that I can set to whatever I want. The AI and Human classes are different in logic but have the same functions and variables. I want to access the functions and variables from both types of players in the array. When searching online I found ‘interface’ and that worked great for the functions, but not for the variables. Since I am kind of a beginner I am probably doing something wrong. Could you please explain how I can access variables in a class from my main sketch?

**My not working code:**

```auto
Player[] players = new Player[]{new PlayerAI(), new PlayerHuman()};

void setup() {
  players[0].printText();
  players[1].printText();
  println(players[0].myVar); // <- Warning here
  println(players[1].myVar); // <- Warning here
}

interface Player { 
  void printText();
  int myVar = 0; // Maybe something like this?
}

class PlayerAI implements Player {
  int myVar = 10;

  void printText() {
    println("I'm an AI");
  }
}

class PlayerHuman implements Player {
  int myVar = 20;

  void printText() {
    println("I'm a human");
  }
}

```

**Output:**

```auto
I'm an AI
I'm a human
0
0

```

**Expected Output:**

```auto
I'm an AI
I'm a human
10
20

```

EDIT:  
I know I can make a function like ‘getMyVar()’ that returns that variable, but I was wondering if I could do it in an easier way without lots of functions.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 25, 2020, 11:30am UTC](https://discourse.processing.org/t/accessing-variables-in-an-array-of-different-objects/17324/2 "2020-01-25T11:30:39Z")

</div>

[https://processing.org/reference/extends.html](https://processing.org/reference/extends.html)

idea is to not declare the variables again ( you could do new ones )  
just set them at call?

---

<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:** [January 25, 2020, 12:00pm UTC](https://discourse.processing.org/t/accessing-variables-in-an-array-of-different-objects/17324/3 "2020-01-25T12:00:20Z")

</div>

```auto
// https://Discourse.Processing.org/t/
// accessing-variables-in-an-array-of-different-objects/17324/3

// 2020-01-25

final Player[] players = {
  new PlayerAI(), 
  new PlayerHuman()
};

void setup() {
  for (final Player p : players) println(p, p.myVar);
  exit();
}

abstract class Player { 
  int myVar;

  String toString() {
    return "I'm a " + getClass().getSimpleName();
  }
}

class PlayerAI extends Player {
  PlayerAI() {
    myVar = 10;
  }
}

class PlayerHuman extends Player {
  PlayerHuman() {
    myVar = 20;
  }
}

```

---

<div class="post-metadata">

**Author:** ![J0R1AN](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/j0r1an/32/7498_2.png) [@J0R1AN](https://discourse.processing.org/u/J0R1AN)\
**Post date:** [January 25, 2020, 2:47pm UTC](https://discourse.processing.org/t/accessing-variables-in-an-array-of-different-objects/17324/4 "2020-01-25T14:47:54Z")

</div>

Just to confirm, I just needed to use abstract and extends instead of interface and implement. Thanks for the help 😃

---

<div class="post-metadata">

**Author:** ![trikaphundo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/trikaphundo/32/7913_2.png) [@trikaphundo](https://discourse.processing.org/u/trikaphundo)\
**Post date:** [January 25, 2020, 11:09pm UTC](https://discourse.processing.org/t/accessing-variables-in-an-array-of-different-objects/17324/5 "2020-01-25T23:09:21Z")

</div>

> [@J0R1AN](#):
>
> Just to confirm, I just needed to use abstract and extends instead of interface and implement

Exactly. Loosely speaking, interfaces specify ‘common behaviour’ for classes—methods—, not properties—member variables—; as you can read in oracle’s [java 8 tutorials](https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html)

> In the Java programming language, an _interface_ is a reference type, similar to a class, that can contain _only_ constants, method signatures, default methods, static methods, and nested types.

So what you tried to do in your code fits best an abstract class, as you want two classes to share member variables.

* * *

> [@J0R1AN](#):
>
> I know I can make a function like ‘getMyVar()’ that returns that variable, but I was wondering if I could do it in an easier way without lots of functions.

Adding a method ‘getMyVar()’ to your classes would have not worked either, as it would not be part of the interface ‘Player’. On the other side, adding such method to the interface seems like a bad decision. So once again, an abstract class ‘Player’ is more suitable than an interface, as @kll has prompted 🙂

* * *

Take a look at this [page on abstract classes](https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html) from the oracle’s java 8 tutorial
