# Parent function not using overwritten array from child

**URL:** <https://discourse.processing.org/t/parent-function-not-using-overwritten-array-from-child/22796>\
**Category:** Beginners\
**Created:** [July 23, 2020, 1:43pm UTC](https://discourse.processing.org/t/parent-function-not-using-overwritten-array-from-child/22796 "2020-07-23T13:43:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NikolafTheGreat](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@NikolafTheGreat](https://discourse.processing.org/u/NikolafTheGreat)\
**Post date:** [July 23, 2020, 1:43pm UTC](https://discourse.processing.org/t/parent-function-not-using-overwritten-array-from-child/22796/1 "2020-07-23T13:43:30Z")

</div>

I am coding an application and ran into an issue where I have created a function in the parent class that references an array also in the parent class. I then overwrite the array in a child class and run the parent function from the child class. I expected the function to use the overwritten array from the child class, but it seems to use the array from the parent class. I am rather confused by this, because this is how it works for simple data types like integers, but apparently not with objects?

Can anyone explain what is happening here and if I can make a workaround that doesn’t involve overwriting the function in every child class? Thank you in advance 😊

```auto
class parrent {
  boolean[] array;

  boolean isArrayTrue(int index) {
    return(array[index]);
  }
}

class child1 extends parrent {
  child1(){}
  boolean[] array = {true, true, true, true, true, true, true};
}

class child2 extends parrent {
  child2(){}
  boolean[] array = {false, true, false, true, false, true, false};
}

class child3 extends parrent {
  child3(){}
  boolean[] array = {false, true, true, false, true, true, false};
}

parrent[] children = {new child1(), new child2(), new child3()};

void setup() {
  for(parrent child : children){
    println(child.isArrayTrue(4));
  }
}

```

#edit 1

I was wrong. I have tested with simple data types. It still uses the parent’s value. It just doesn’t throw an exception when I use simple data types that I haven’t assigned a value and just uses a standard value.

Then let me rephrase my question. Is there a way to make a parent function reference a variable that has been overwritten a by a child when called from the child object?

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 23, 2020, 2:34pm UTC](https://discourse.processing.org/t/parent-function-not-using-overwritten-array-from-child/22796/2 "2020-07-23T14:34:28Z")

</div>

Hi,

You need to redefine your function in every chidren.

After there is something I don’t know in JAVA. In c++ when you store your child as a parrent (which you do with `parrent[] children`) you need to declare your function as `virtual` so it uses the children version of the function and not the parent version.

Not sure if that’s the case in JAVA.

EDIT: You don’t need the `virtual` keyword: [https://www.jitendrazaa.com/blog/java/virtual-function-in-java/#:~:text=In%20Java%20there%20is%20no,to%20provide%20the%20polymorphic%20behavior](https://www.jitendrazaa.com/blog/java/virtual-function-in-java/#:~:text=In%20Java%20there%20is%20no,to%20provide%20the%20polymorphic%20behavior).

---

<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:** [July 23, 2020, 3:26pm UTC](https://discourse.processing.org/t/parent-function-not-using-overwritten-array-from-child/22796/3 "2020-07-23T15:26:41Z")

</div>

> [@NikolafTheGreat](#):
>
> I then overwrite the array in a child class and run the parent function from the child class.

You’re not overriding but overshadowing the parent’s class field named _array_ inside each of its child classes!

Overriding is for non-static methods only. Other types of class members become overshadowed instead.

So when subclassing take extra care not to accidentally re-declare inherited fields and static methods.

If you need so instead assign a value w/o re-declaring them.

```auto
// https://Discourse.Processing.org/t/
// parent-function-not-using-overwritten-array-from-child/22796/3

// GoToLoop (2020/Jul/23)

final Parent[] children = { new Child1(), new Child2(), new Child3() };

void setup() {
  printArray(children);
  println();
  for (final Parent child : children) println(child.isArrayTrue(4));
  exit();
}

abstract class Parent {
  boolean[] array;

  boolean isArrayTrue(final int index) {
    return array[index];
  }

  @Override String toString() {
    return java.util.Arrays.toString(array);
  }
}

class Child1 extends Parent {
  {
    array = new boolean[] { true, true, true, true, true, true, true };
  }
}

class Child2 extends Parent {
  {
    array = new boolean[] { false, true, false, true, false, true, false };
  }
}

class Child3 extends Parent {
  {
    array = new boolean[] { false, true, true, false, true, true, false };
  }
}

```

---

<div class="post-metadata">

**Author:** ![NikolafTheGreat](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@NikolafTheGreat](https://discourse.processing.org/u/NikolafTheGreat)\
**Post date:** [July 24, 2020, 9:12am UTC](https://discourse.processing.org/t/parent-function-not-using-overwritten-array-from-child/22796/4 "2020-07-24T09:12:22Z")

</div>

Thank you very much. This solved my problem. 👌
