eclipse
eclipse

Reputation: 3011

Making Object immutable when passed as parameter

Can I make an Object immutable when passed as a parameter, so that the called method can't change it but the callee can?

So I have somthing like this:

Class Car {
  Wheel wheel_1;

  Axis axis = new Axis(wheel_1);
}


Class Wheel {
  int size;

  setSize(int size) {}
  int getSize() {}      
}

Now I construct a car with a wheel. Then from class car I want to construct an axis.

For that I pass wheel_1 to the constructor of Axis.

Now my question: Can I asure somehow that the constructor of Axis doesnt change the size of wheel_1 but class car can change it.

Upvotes: 7

Views: 7862

Answers (6)

ed_me
ed_me

Reputation: 3508

I would have Wheel implement an interface that has only accessor methods, i.e: -

Example

interface WheelInterface
{
  int getSize();
}

class Wheel implements WheelInterface
{
  // methods
}

class Axis
{
  public Axis(WheelInterface wheel)
  {
    // Only getSize will be available
  }
}

Now, simply pass WheelInterface instead of Wheel and only the accessor methods will be available to the constructor of your Axis class

Benefits

The benefits of doing this is that there is no copying required; you are simply providing a contract to the Axis class and that contract states that it can only get the size, not change it.

By passing the same object reference by value, you aren't having to call any copy constructor and don't have to worry about deep and shallow copying semantics. I would not want to use Clone on my wheel, I think that feels a little dirty for reasons mentioned in comments of other answers.

In an object-oriented pattern, using an interface to abstract away what you don't need is also a sign of good design. If your wheel has snow tyres on, your Axis class probably doesn't even need to care!

Drawbacks

As others have mentioned, it is possible to cast the interface back to a concrete type (as others have mentioned, assuming you know what you're casting to); this is called downcasting and I don't recommend it; if this were done in a similar scenario, it probably wouldn't get past a code review.

Upvotes: 3

Duncan Jones
Duncan Jones

Reputation: 69329

Yes. Typically this is done by utilising a copy-constructor for the Wheel class.

For example:

wheel_1 = new Wheel(wheel);

Bear in mind, the Wheel class will need to be written in a way that supports this. That is, it should either offer a copy-constructor.

Note: this hasn't made the object immutable. It has merely produced a copy that can't be edited by anything outside your class.

If you return the Wheel instance from any of your other methods, be sure to defensively copy it on the way out too:

Wheel getWheel() {
  return new Wheel(wheel_1);
}

Finally, it's worth mentioning that it's always a good idea to create immutable classes whenever you can. So perhaps you can avoid this issue by actually making Wheel immutable?

Upvotes: 7

maba
maba

Reputation: 48045

Make the Wheel class immutable. Then let the Car object create a new Wheel object when it needs a new size.

public final class Wheel {
    private final int size;

    public Wheel(int size) {
        this.size = size;
    }

    public int getSize() {
        return size;
    }
}

Now you can pass the wheel object to the Axis without any problems.

public class Car {
    private Wheel wheel;
    private Axis axis;

    public Car(int initialWheelSize) {
        wheel = new Wheel(initialWheelSize);
        axis = new Axis(wheel);
    }
}

Upvotes: 4

IndoKnight
IndoKnight

Reputation: 1864

Yes, you can. If you make Car and Wheel in the same package and declare Wheel setter fields to be protected. So, Axis if declared in another package can't access setters of Wheel, thus making Wheel immutable from Axis point of view.

Well, this solution is basic to start with. Ofcourse, you can think of whether to make Wheel final or not, cloning, serializable, etc. based on what level you want to make immutable.

Upvotes: 1

ripu1581
ripu1581

Reputation: 300

Why don't you send the parameter as its superclass which doesn't have setSize() method

Class Car {
  Superwheel wheel_1;

  Axis axis = new Axis(wheel_1);
}

class Superwheel
{
   int size;
 int getSize() {}    
}

Class Wheel extends Superwheel{
  int size;

  setSize(int size) {}
  int getSize() {}      
}

Upvotes: 2

Gabriel Negut
Gabriel Negut

Reputation: 13960

Pass the Axis constructor a copy of wheel_1.

class Car {
    Wheel wheel_1;

    Axis axis = new Axis(new Wheel(wheel_1));
}

Also, Wheel will need a constructor which takes another Wheel object and copies its properties.

Upvotes: 3

Related Questions