One class included to other class by composition. How to change it's private fields?

I have simple class:

public class Points {

    private int x = 0;

    public void setX(int x) {
        this.x = x;
    }
}

And I make other class which have a Points field:

public class Curve {

    private Points p;

    public void setX(int x) {
        p.setX(x);
    }
}

So, setX(int x) method in class Curve is used to change value of private field x in Points.

Finally I use this method in such way:

public class UseComposition {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Curve c = new Curve();
        c.setX(1);
    }

}

I think, this code have a doubling: if I change a method name in Points I also have to change method setX in class Curve. Are there other ways to solve such task? May be it isn't a good idea at all to change such private values. Please, help.

Jon Skeet
people
quotationmark

I think, this code have a doubling: if I change a method name in Points I also have to change method setX in class Curve.

No you don't. You can if you want, but the two methods are independent. You don't have to expose all the methods within Points at all, and indeed most of the time you wouldn't want to. You should only expose methods which are relevant to the class you're writing. Sometimes they may just delegate down via a single field, but often they'll involve multiple fields, or you'll need to do other things as well as calling the delegated method.

people

See more on this question at Stackoverflow