# How can I access object method inside other object?

**URL:** https://discourse.processing.org/t/how-can-i-access-object-method-inside-other-object/4609
**Category:** Coding Questions
**Created:** [October 18, 2018, 4:54pm UTC](https://discourse.processing.org/t/how-can-i-access-object-method-inside-other-object/4609 "2018-10-18T16:54:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![enonod](https://avatars.discourse-cdn.com/v4/letter/e/85e7bf/32.png) [@enonod](https://discourse.processing.org/u/enonod)
#### Post date: [October 18, 2018, 4:54pm UTC](https://discourse.processing.org/t/how-can-i-access-object-method-inside-other-object/4609/1 "2018-10-18T16:54:01Z")

</div>

Within DPanel I have added two objects, captionBar and btnClose and have called their display() methods successfully as shown in DPanel.display(). I have attempted to bring in a third object via the DPanel method addObject. However I get the message that display() does not exist… referring to the Existing\_Object.display().

The Existing\_Object has a display() method no different to the two objects within DPanel. I therefore assume that the invisibility is caused by my attempt to ‘import’ a pre-existing object to make it part of DPanel. Can anybody please explain, simply, what it is that I don’t understand. Thank you.

```auto
class DPanel {
//variable declarations
    ...
    PFont arial14;
    Object ExistingObject;

//Constructor
    DPanel(int _x, int _y etc.) {

    btnClose = new DButton(x, y, etc.);
    captionBar = new DLabel(x, y, etc.);

  }//end constructor

//Methods
    void addObject(Object _Existing_Object) {
    Existing_Object = _Existing_Object;
}// end addObject

  void display() {
    fill(0);
    text(txt, etc...);
    captionBar.display();
    btnClose.display();
    Existing_Object.display();
  }// end display

}//end DPanel

```

---

<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: [October 18, 2018, 5:18pm UTC](https://discourse.processing.org/t/how-can-i-access-object-method-inside-other-object/4609/2 "2018-10-18T17:18:03Z")

</div>

The problem is that you have declared `ExistingObject` to be of type `Object` which is the base class for all Java and user-defined classes. So in the statment `Existing_Object.display();` will look for a display method inside the class `Object` and will not find one.

---

<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: [October 18, 2018, 5:33pm UTC](https://discourse.processing.org/t/how-can-i-access-object-method-inside-other-object/4609/3 "2018-10-18T17:33:24Z")

</div>

Create an Interface like this

```auto
public Interface Displayable {
  public void display();
}

```

then declare the DPanel class

```auto
public class DPanel implements Displayable {

PFont arial14;
Displayable ExistingObject;

//Constructor
DPanel(int _x, int _y etc.) {

btnClose = new DButton(x, y, etc.);
captionBar = new DLabel(x, y, etc.);

}//end constructor

  //Methods
  void addObject(Displayable _Existing_Object) {
    Existing_Object = _Existing_Object;
  } // end addObject

  void display() {
    fill(0);
    text(txt, etc…);
    captionBar.display();
    btnClose.display();
    Existing_Object.display();
  } // end display

} //end DPanel

```

All your GUI control class (e.g. DButton, DLabel etc) should also implement `Displayable`

---

<div class="post-metadata">

### Author: ![enonod](https://avatars.discourse-cdn.com/v4/letter/e/85e7bf/32.png) [@enonod](https://discourse.processing.org/u/enonod)
#### Post date: [October 18, 2018, 5:39pm UTC](https://discourse.processing.org/t/how-can-i-access-object-method-inside-other-object/4609/4 "2018-10-18T17:39:12Z")

</div>

Thank you for your very quick reply. I have attempted to respond via the forum but the web page is constantly blank.  
Will you please be kind enough quark, to suggest what I should do instead. This is not just a mistake on my part, I actually do not know even if it is possible to add an object this way nor how.  
Thank you

---

<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: [October 19, 2018, 7:47am UTC](https://discourse.processing.org/t/how-can-i-access-object-method-inside-other-object/4609/5 "2018-10-19T07:47:55Z")

</div>

> [@enonod](#):
>
> Will you please be kind enough quark, to suggest what I should do instead.

I have - see my second post!

---

<div class="post-metadata">

### Author: ![enonod](https://avatars.discourse-cdn.com/v4/letter/e/85e7bf/32.png) [@enonod](https://discourse.processing.org/u/enonod)
#### Post date: [October 19, 2018, 8:54am UTC](https://discourse.processing.org/t/how-can-i-access-object-method-inside-other-object/4609/6 "2018-10-19T08:54:41Z")

</div>

Maybe the web site goes off at night, it is back now 🙂  
Many thanks quark for the introduction to something completely unknown to me. I will now play for a while and hopefully succeed
