# Change a Class without changing it’s code?

**URL:** https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748
**Category:** Coding Questions
**Created:** [October 22, 2018, 1:15pm UTC](https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748 "2018-10-22T13:15:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 22, 2018, 1:15pm UTC](https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748/1 "2018-10-22T13:15:10Z")

</div>

I have the following problem, i Need to change a Class that i have to Import from outside, But i can only Import and not Access it to change it. I also can‘t (could, But is much more complicated Solution) extend a new class from this class, because this class is used in another class, where i still Need it to go through to make everything go how i Need it. And i just Need to add a Variable and add a new function. Don‘t Need to change other things (as far as i know right now). So, in short : I Need to add things to a Class i can‘t Access, But i can‘t extend from it. What can i do?

---

<div class="post-metadata">

### Author: ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)
#### Post date: [October 22, 2018, 5:12pm UTC](https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748/2 "2018-10-22T17:12:12Z")

</div>

Can I ask why you can’t access it?

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 22, 2018, 10:14pm UTC](https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748/3 "2018-10-22T22:14:50Z")

</div>

It is an Imported class within one of processings internal classes and is from org.eclipse, so i can’t Access it to change it… (if i‘m not mistaken on how that works 😅)

---

<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 22, 2018, 10:24pm UTC](https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748/4 "2018-10-22T22:24:25Z")

</div>

Not sure if this is what you want, but you can create you own class `class myClass extends importedClass {}` and create your custom functions.

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 23, 2018, 7:38am UTC](https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748/5 "2018-10-23T07:38:28Z")

</div>

As i said, i can‘t (could, But would have me Chance around 3000 Lines of code…) extend a new class from the imported one 😅

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 24, 2018, 6:02pm UTC](https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748/6 "2018-10-24T18:02:49Z")

</div>

> [@Lexyth](#):
>
> And i just Need to add a Variable and add a new function. Don‘t Need to change other things (as far as i know right now). So, in short : I Need to add things to a Class i can‘t Access, But i can‘t extend from it. What can i do?

One approach is to use the [Decorator pattern](https://en.wikipedia.org/wiki/Decorator_pattern) to add without changing. This very much depends on all the specifics of what you are trying to do that you aren’t sharing, so I can’t give you more detail without more information, but the basic idea of [composition](https://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition) is to stick your unchangeable object as a property in a wrapper class, and add your one variable and one new function there. Whether you then need to forward everything from the underlying object or implement it as an interface etc. again depends on the details of what you are try to do.

But, to start with a basic wrapper approach (no interface forwarding):

```auto
class MyClass {
  public CantChange wrapped;
  private int val = 3;
  MyClass(CantChange wrapped){
    this.wrapped = wrapped;
  }
  public int getVal() {
    return val;
  }
}

CantChange unchangeable = new CantChange();
MyClass mc = new MyClass(unchangeable);

mc.getVal(); // 3

mc.wrapped.normalMethod(); // as usual for a CantChange

functionThatTakesACantChange(mc.wrapped); // as usual for a CantChange

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 25, 2018, 9:51am UTC](https://discourse.processing.org/t/change-a-class-without-changing-it-s-code/4748/7 "2018-10-25T09:51:22Z")

</div>

Thanks a lot 😅 that’s what i was looking for 😊
