Games
Problems
Go Pro!

Introduction to Object Oriented Programming

Reference > Science > Technology > Object Oriented Programming
 

The purpose of this unit is to provide a very basic explanation of the concepts in Object Oriented Programming, without actually linking the explanations to any specific programming language. The explanations will therefore serve as a good introduction even for people who have never learned a programming language, and are not familiar with any specific language structure and syntax.

In the early days of programming computers, language was largely structured around commands designed to perform specific tasks.

For example, suppose you wanted to calculate the volume of a sphere, if you knew the radius was 2 units. You know that the volume of a sphere is
4
3
πr3. So you would give the computer a series of commands like this:

Command
Response
Raise 2 to the 3rd power
8
Multiply that by π
25.13
Multiply that by
4
3
33.51

That's all well and good, but what if you've got a whole bunch of spheres, and you want to find all their volumes; are you going to write that same set of code over and over again for each sphere? NO! You're going to make your list of commands reusable - and this is function based programming. In function based programming, you create a set of instructions that you want to be able to use over and over again, and you make it a function - just like math functions that you can pass numbers into. Like this:

SPHERE VOLUME FUNCTION

Command
Response
Raise r to the 3rd power
8
Multiply that by π
25.13
Multiply that by
4
3
33.51

The only thing that's different is, instead of the number 2 in the first command, we've got a variable. Now, if we have three spheres, with radii 1, 2, and 3, we can do this:

Command
Response
Run the Sphere Volume function with r = 1
4.19
Run the Sphere Volume function with r = 2
33.51
Run the Sphere Volume function with r = 3
113.1

Notice that it LOOKS like we've only run three commands - one for each sphere. But in reality, we've run 12 commands - the three commands to run the volume function, plus the three commands in the function each time.

So we've made our program more succinct by creating a function that is sort of like a work horse - once we've created it, we can just keep running it over and over again with different numbers.

Object Oriented Programming takes things a step further than function based programming. In OOP, our goal is - in a sense - to teach the computer what a sphere is, and then create "virtual spheres" for the computer to work with.

We teach the computer that spheres have a radius, and then we teach the computer that it also has things like diameter, surface area, and volume (we teach it how to find the diameter and the surface area the same way we taught it how to find the volume - with a function).

Then we can do something like this:

Command
Response
Create a sphere with radius 5 and call it S
S has been created
Tell me the diameter of S
10
Tell me the surface area of S
314.2
Tell me the volume of S
523.6

Notice how convenient this is - once we've taught the computer what a sphere object is, we don't have to keep track of the nuts and bolts. In fact - we don't even need to keep track of the radius - check this out:

Command
Response
By the way - what was the radius of S?
5

In the old function based way of doing things, we had to keep track of the radii of all our spheres, because we had to pass that number to each function every time we wanted to find out something about the sphere. Now, since the computer has been taught what a sphere is, and has created a virtual sphere, it's keeping track of all the info about that sphere for us. And instead of us having to always tell the computer information about the sphere, the computer can tell us about it.

That, in a very simplistic nutshell, is the premise of Object Oriented Programming.

Questions

1.
Why is function based programming better than command based programming?
2.
Why is Object Oriented programming better than function based programming?
3.
If I told the computer to create a sphere with radius 8, and asked the computer for the diameter, what would it tell me?
Assign this reference page
Click here to assign this reference page to your students.
Unit IndexUnit Index
Creating an Object - Basic AttributesCreating an Object - Basic Attributes
 

Blogs on This Site

Reviews and book lists - books we love!
The site administrator fields questions from visitors.
Like us on Facebook to get updates about new resources
Home
Pro Membership
About
Privacy