SilentDev
SilentDev

Reputation: 22747

Actionscript: What good does using private variables do?

So I am reading the Actionscript 3 tutorials here: http://www.adobe.com/devnet/actionscript/learning/oop-concepts/writing-classes.html

and I want to know, how is Rectangle version three better (more resuable and flexible) than rectangle version two? I see that in version three, they create private variables and then change the private variables to variables in the constructer method but then they go back and use the private variables in draw().. what good does that do?

Upvotes: 1

Views: 82

Answers (2)

Panzercrisis
Panzercrisis

Reputation: 4750

Imagine buying a brand new TV. You have a remote, you have a few buttons on the TV, some jacks, a cord or two, speakers, and a screen. Now all you have to do is just get that TV plugged up, and you're ready to go, right?

Do you need to know how the TV works internally? Do you need to know whether it has an electron gun, a liquid crystal display, or some other means to convery the image? Do you need to know how many fans, buses, wires, switches, logic gates, etc. are in there? ... Do you need to know how to make a TV yourself?

No. And the fact that all these sorts of details, bits, pieces, and parts are hidden behind what is essentially a black, plastic box is not just for aesthetic pleasure - it's to keep the customer from getting into stuff they shouldn't.

If you're the manufacturer, and if you leave the TV's circuit boards on the outside of the TV, not on the inside, some customers who don't know what in the world they're doing are going to mess with those circuit boards and accidentally screw up the TV. Furthermore having to worry about whether to mess with those circuit boards or not is going to cost them time in learning how to use that particular model of a TV. So you keep the circuit boards, wires, and all sorts of other stuff packaged neatly inside that black, plastic box - away from the eyes of the customer. All they need (and should) worry about is getting used to their new remote.

This is not to say, mind you, that the customer can't just get out a screwdriver and start pulling this stuff out anyway. This is just to say that by keeping those things tucked away like that, even if the customer does open up the TV and mess around with the internal workings, they'll at least know they're treading on dangerous ground.

Private variables and methods are like the bits and pieces inside of the TV, whereas public ones are things like the speakers, monitor, and remote control. You make the distinction for the same exact reasons. In most cases the programmer you're tucking these details away from (a lot of times it's you guarding it against yourself and your own careless errors) can still see what you've got there, and they can still get to it and mess with it if they need to, but the keyword "private" gives them a very solid realization that "this variable/method belongs to this class and this class alone, and it should not be tampered with anywhere outside of it". If they do try to access it from something outside of that class's code, it just won't compile basically.

Upvotes: 2

moosefetcher
moosefetcher

Reputation: 1901

Keeping variables private is all about 'encapsulation'. If most of a Class's code is private any code that uses that Class doesn't need to know how it implements its methods. This means that, so long as the method names and parameters stay the same, you can change how a Class implements methods without changing anything OUTSIDE the Class. It also prevents 'tight coupling'; where Classes come to depend on other Classes.

Upvotes: 4

Related Questions