0

what would be the best way to go about this? For instance, consider the following code:

public class ObjectA {
    public int data;

    public ObjectA() {
        this.data = 1;
    }

    public changeData(int x) {
        this.data = x;
    }
}

public class ObjectB {
    public int data;

    public ObjectB() {
        this.data = 1;
    }

    public changeData(int x) {
        this.data = x;
    }
}

How can this be refactored so changeData can have both classes can use it? Also, so that changeData will be safe to use.

plsplox
  • 179
  • 2
  • 11

1 Answers1

1

You could create your helper function and data as part of a base class and derive all other classes from it.

pasha
  • 2,035
  • 20
  • 34
  • I wish- just joined a project with a pretty large code base. In our case, ObjectA, ObjectB, and other similar classes are already extending other classes. – plsplox Jul 31 '17 at 18:40
  • You could create static templatised member functions of a helper class... Apologies for not being specific but the question seems a bit less specific and if I suggest something specific it could have pitfalls... I'm just trying to give you some ideas – pasha Jul 31 '17 at 19:12
  • no worries, sorry for being too general! Will try your suggestion, thank you! – plsplox Jul 31 '17 at 20:04