Methods - Changing an Object's Properties
Reference > Science > Technology > Object Oriented ProgrammingSo far in this unit on Object Oriented Programming, we've learned about what an object is, and what an object's properties are. In this page, we'll talk about modifying an object once we've created it.
Let's continue with our sphere object. Let's say we've told the computer "Create a sphere object with radius = 2, mass = 10, and call it K."
(I changed from S to K, just to remind you that we can name our objects anything we want to.)
So far it's not a terribly interesting object - it doesn't do anything; we can just get information about it. But let's ask ourselves one simple question: what are some things we could DO to the sphere?
Well, we could squish it, we could stretch it, we could paint it...
Of course, if we squished it, it wouldn't be a sphere any more, would it? And none of our properties that we taught the computer would be accurate any more. So let's not have a "squish" method!
Similarly, "stretch" might not be a good idea either, since it would become more like an ellipsoid if we stretched it. But we could expand it, and it would still be a sphere. Similarly, we could paint it, and it would still be a sphere.
Great! So our sphere will have two things we can do to it. We'll call these two things "methods."
All sphere objects will have an "EXPAND" method and a "PAINT" method. But we need to teach the computer what those methods mean.
So we tell the computer: "To EXPAND a sphere by x, add x to its radius."
Now if I tell the computer "Expand K by 5," the computer checks to see what the radius of K is (it's 2, because that's what we told it in the creation command), and then adds 5 to that.
Now if I say to the computer, "What's the radius of K?" the computer won't say "2," it'll say "7," because the method has changed the properties of the sphere. Similarly, if I ask, "What's the density of K?", the computer will recalculate the density based on the new radius of the sphere.
But what about painting the object? The PAINT method is going to do two things*: it's going to change the color of the sphere, and it's going to increase the sphere's radius by a small amount. So I need to teach the computer this:
"To PAINT a sphere color X, change its color to X, and increase its radius by 0.01."
So if I now say to the computer, "Paint K blue," it will change the sphere's color property to "blue," and it will increase the sphere's radius from 7 to 7.01.
If I then say "Paint K red, then paint K orange," the end result will be that the sphere is orange, and its radius is now 7.03.
Again, it's all encapsulated in that one set of instructions about spheres, and if, five years down the road I can't remember how much the radius is increased when a sphere is painted, it doesn't matter - the computer will take care of it automatically every time I give the PAINT command.
* Technically, painting it will also increase its mass, since the paint isn't weightless, but that requires a bit more complicated math than I want to go into on these introductory pages, so we'll just pretend it's weightless paint!