# Getter / Setter being taken advantage of

**URL:** <https://forum.kirupa.com/t/getter-setter-being-taken-advantage-of/263041>\
**Category:** flash\
**Created:** [June 12, 2008, 3:11am UTC](https://forum.kirupa.com/t/getter-setter-being-taken-advantage-of/263041 "2008-06-12T03:11:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 12, 2008, 3:11am UTC](https://forum.kirupa.com/t/getter-setter-being-taken-advantage-of/263041/1 "2008-06-12T03:11:25Z")

</div>

Okay, an interesting question that I haven’t found a definitve answer for yet (but I’m sure there is one out there ;))

If I have a class that has an object such as a Point that uses getter / setter methods to set it, I seem to be able to bypass the setter and rely only on the getter to get _and_ set, which is a little strange.

For example, pretend this is part of a class called PointTest:

```auto
private var myPoint:Point = new Point();
 
public function get thePoint ():Point
{
     return myPoint;
}

```

And then elsewhere…

```auto
var myPointTest:PointTest = new PointTest();
 
myPointTest.thePoint.x = 50;

```

The above works, even though no setter is defined. It seems to bypass the setter and set through the getter, which is… well, not quite what I was expecting 😉

Any thoughts on how I might try and control this? An alternative would be to have getter/setters for the X and Y values only that set the point’s X and Y internally, but that’s a little messy. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 12, 2008, 4:52am UTC](https://forum.kirupa.com/t/getter-setter-being-taken-advantage-of/263041/2 "2008-06-12T04:52:02Z")

</div>

Yeah, I agree that it’s odd. You’ll notice that it is in fact protecting the actual point - you can’t set it to null for example, or to another point, but you can adjust the individual values of the point - which I agree does not properly protect the encapsulation.

I think you’ll have to protect the entire thing and adjust the variables individually, like this:

```auto

package
{
	import flash.geom.Point;
	
	public class SubclassTest
	{
		private var _myPoint:Point;
		
		public function SubclassTest()
		{
			_myPoint = new Point();
		}
		
		public function get pointX():int
		{
			return _myPoint.x;
		}
		
		public function set pointX(i:int):void
		{
			_myPoint.x = i;
		}
	}
}

```

I agree that it isn’t ideal, but it does protect the encapsulation and it works. Maybe someone else has a better answer. :-\

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 12, 2008, 5:47am UTC](https://forum.kirupa.com/t/getter-setter-being-taken-advantage-of/263041/3 "2008-06-12T05:47:52Z")

</div>

It’s not really that odd since you have a get function which returns a reference to that point in memory and therefore allows you to access it. The protection of \_myPoint’s properties would be handled by \_myPoint (and the class by which it is defined). You could create a subclass of point which uses getters and setters for its properties.

Personally I don’t see the point of using get/set unless you wish to perform some sort of modification to the data before returning or setting the property. Example:

```auto
public function get red():uint {
	return (this.n >> 16) & 0xFF;
}
public function set red(n:uint):void {
	if(n > 255) throw new RangeError("Colour channel cannot exceed 255");
		this.n = this.alpha << 24 | n << 16 | this.green << 8 | this.blue;
	}
}

```

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 12, 2008, 7:12am UTC](https://forum.kirupa.com/t/getter-setter-being-taken-advantage-of/263041/4 "2008-06-12T07:12:49Z")

</div>

That’s exactly why I’m using a get/set, Mr Canadian 🙂

I’m writing some game code at the moment, and am using “virtual” numbers (stored as Points) to calculate a player character’s next position before committing to it. When a wall hit is detected, it locks the ability to change the player’s movement and bounces the player in the opposite direction to the one they were travelling in. It unlocks the ability to move the player again once the character has reached a standstill.

I was hoping to simplify access to the internal points by using a setter, but as you said the getter returns a “live” reference to the internal Point, allowing it to be modified and thus bypassing any setter. Ah well. I’m going with the way Anogar and I thought of, which is just to use getter/setters for each x/y pair. It does mean a bit of duplicated code, but it does also mean the code works 🙂

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 12, 2008, 7:47am UTC](https://forum.kirupa.com/t/getter-setter-being-taken-advantage-of/263041/5 "2008-06-12T07:47:12Z")

</div>

> [@TheCanadian;2341199](#):
>
> Personally I don’t see the point of using get/set unless you wish to perform some sort of modification to the data before returning or setting the property.

A public getter on a private variable is a good way to protect the encapsulation of your class. I see empty getters / setters just as a way to save myself time if during the course of a project I want to protect the way a variable is set, so I typically use them for properties that I plan on having external classes mess with.
