15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

240<br />

Sharing Code with Inheritance<br />

Single vs. multiple inheritance<br />

When one object inherits everything from<br />

another object, that’s single inheritance. Some<br />

<strong>programming</strong> languages, such as C++, also can<br />

allow objects to inherit from two or more<br />

objects, which is multiple inheritance:<br />

Good news: Multiple inheritance can use<br />

the best parts from two or more objects<br />

and smash them together to create a new<br />

object.<br />

Without multiple inheritance, you can only<br />

inherit code from one object and then you<br />

must duplicate code from a second object.<br />

Because duplicating code is what objectoriented<br />

<strong>programming</strong> tries to avoid in the<br />

first place, multiple inheritance is yet<br />

another way to make creating objects easy<br />

and fast by reusing existing code from<br />

other objects.<br />

Bad news: Multiple inheritance can make<br />

programs harder to understand.<br />

By inheriting parts from so many objects, an<br />

object can become a hodge-podge collection<br />

of parts from everywhere. So a single<br />

object that inherits code from multiple<br />

objects is not only harder to understand, but<br />

also dependent on too many other parts of<br />

a program. Such interdependency of code<br />

is a problem object-oriented <strong>programming</strong><br />

tries to eliminate in the first place.<br />

If your <strong>programming</strong> language offers multiple<br />

inheritance, try it to see if its benefits outweigh<br />

its drawbacks. If your <strong>programming</strong> language<br />

doesn’t offer multiple inheritance, don’t feel that<br />

you’re missing out on anything because most<br />

<strong>programming</strong> languages don’t offer multiple<br />

inheritance.<br />

Sound like a lot of trouble? It is, which is what makes inheritance so attractive.<br />

With inheritance, you don’t make multiple, physical copies of subprograms.<br />

Instead, you first store the subprogram in an object (a class file).<br />

Next, you inherit that object. Inheritance tells the computer to copy an<br />

object (along with all the subprograms stored inside that object) and store<br />

all the data and subprograms from that first object into that second object.<br />

Physically, this second object contains no code of its own. Instead, the<br />

second object points to the code of the original object, as shown in Figure 7-7.<br />

When you run subprograms in this second object, this second object tells<br />

the computer, “Hey, those subprograms are really stored in this other object<br />

that I inherited them from.”<br />

Inheritance offers two advantages:<br />

✦ Because it doesn’t make multiple copies of the same subprograms,<br />

inheritance saves space.<br />

✦ Because only one copy of a subprogram physically exists, inheritance<br />

makes it easy to update a subprogram.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!