Games
Problems
Go Pro!

Object Inheritance

Reference > Science > Technology > Object Oriented Programming
 

In the normal way of using the word, an "inheritance" is something you receive when someone passes away - maybe they left a sum of money for you in their will, or some property - that money or property is your inheritance.

In Object Oriented Programming, the word inheritance means something different - after all, none of our objects have died and given anything to any other objects!

However, the word inheritance does still carry on the idea of passing on something from one object to another. In most cases, the things passed are properties and methods that are passed from one type of object to another.

Let's go back to our sphere example. Remember that on previous pages we defined a sphere object by telling the computer its radius, and mass, and then we taught the computer how to calculate other properties like volume and density. Then we taught the computer what it means to "expand" a sphere, and what it means to "paint" a sphere.

But what if we wanted to start talking about a specific kind of sphere. Maybe we're not interested in just any old sphere - maybe we're talking about pool balls. Do all the properties of spheres apply to pool balls? Absolutely! They have a radius, a mass, and a color (though in the case of a pool ball, they have two colors, one of which is white, and their paint might be either solid or striped). Do all the methods of spheres apply to pool balls? Well, yes, I suppose we could expand a pool ball (not sure how, but it's fun to imagine!) and yes, we could paint it - although if we did either of these things, the pool ball would no longer be "tournament legal," and wouldn't be good for much.

Nevertheless, a pool ball is a type of sphere, so we say that it "inherits" the properties and methods of a sphere.

So if we're going to work with pool balls, we need to tell the computer all about pool balls. We start by explaining to the computer that a pool ball is a type of sphere. Just like that, the computer knows a lot about any pool ball we create!

But now we have to ask ourselves, does a pool ball have any extra fundamental properties that a simple sphere doesn't? Well, a pool ball has a number, and it has either a striped or solid pattern. The number of the ball determines whether or not it's striped (numbers less than 9 are solid), so number is the only fundamental property.

Also, the color of the ball is dependent on the number: if it's an eight, it's black, if it's a five, it's orange, etc. In fact, we could create a whole table of color/number combinations for the computer to use, so when we specify a number, the computer can look up the color.

Now we need to teach the computer how to create a cue ball: "To create a cue ball, create a sphere with radius 1.115 and mass 0.012*, and the number X. If X > 8; if it is, set the STRIPED property to YES; otherwise set it to NO.  Now look up X in our color table, and PAINT the ball to the correct color."

* A standard pool ball has a radius of 1.125 inches, and mass of 0.012 slugs. Why do we set the radius to 1.115 instead of 1.125? Because the first thing we're going to do to the sphere is to PAINT it, which will increase its radius by 0.01, making it the proper standard size!

Here's something important to notice: when we create the pool ball, we don't have to specify a radius and mass; those will be set automatically since they are standard for all pool balls. The ONLY thing we need to tell the computer is the number, and from this, the computer sets all the rest of the properties for us! So once we've taught the computer how to create a pool ball, we can just tell the computer "Create pool ball #5." The computer will create sphere object (with the right mass and radius), set the STRIPED property to NO, look up 5 in our color chart and paint it orange.

Now, instead of creating a Sphere object, we've created a Pool Ball object. But the nice thing is, since we've told the computer that a pool ball is a type of sphere, we don't have to teach it all over again how to find the pool ball's volume, or its density, or its surface area; the computer can use the properties we already taught it for spheres - properties that the pool ball inherited from the sphere.

If you wanted to get really precise about things, there's another property of the pool ball, and I alluded to it earlier; a pool ball can either be "legal" or "not legal." If it's the wrong size, the wrong density, the wrong color, or has a number that's not an integer between 1 and 15, it's not legal.

So we could tell the computer that pool balls have a property called "LEGAL," which has a value of either YES or NO. How does that change our object? In a few ways, actually. One way is that if we call the "EXPAND" method or the "PAINT" method, we need to have the computer check the radius to make sure it's a legal radius. Also, if the pool ball was created with a number like 3.14 or 16, the computer needs to set the LEGAL property to NO.

Could you use inheritance to create other kinds of spheres? 

Sure! You could create ping pong balls, planets, suns, and all kinds of spheres, each with its own set of extra properties.

This is far from a complete tutorial on Object Oriented Programming - there are more advanced topics like polymorphism, multiple inheritance, read-only, public, private, and protected methods and properties, and much more, which we haven't even touched, but this is enough to give you a very basic concept of how object oriented programming works.

Each programming language has its own way of implementing OOP, and some are more robust than others; some allow for multiple inheritance, and some do not, and some languages (like Javascript) have very round-about methods for implementing OOP. But harnessing the power of OOP will make you a much better programmer, regardless of the language you're using!

Questions

1.
What are some additional properties a ping pong ball might have?
2.
What's another kind of sphere you might want to create an object for?
3.
For the type of sphere you gave in the previous question, what are some extra properties and methods it might have?
4.
If you painted the pool ball 5 times after it was done being created, by what (negative) value would you need to expand the sphere in order for its LEGAL property to be set?
Assign this reference page
Click here to assign this reference page to your students.
Methods - Changing an Object's PropertiesMethods - Changing an Object's Properties
Unit IndexUnit Index
 

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