Chapter 6. Inheritance

In object-oriented programming, inheritance is a formal relationship between two or more classes, wherein one class borrows (or inherits) the variable and method definitions of another class. In the practical, technical sense, inheritance simply lets one class use the code in another class.

But the term inheritance implies much more than code reuse. Inheritance is as much an intellectual tool as it is a technical tool. It lets programmers conceptualize a group of classes in hierarchical terms. In biology, inheritance is a genetic process through which one living creature passes on traits to another. You are said to have inherited your mother's eyes or your father's nose, even though you don't look exactly like either of your parents. In object-oriented programming, inheritance has a similar connotation. It lets a class look and feel in many ways like another class, while adding its own unique features.

This chapter begins by examining the syntax and general use of inheritance. Once we understand inheritance on a practical level, we'll consider its benefits and alternatives. Finally, we'll apply inheritance to our virtual zoo program.

A Primer on Inheritance

Let's consider a very simple, abstract example to get a feel for how inheritance works (we'll get into practical applications once we cover the basic syntax). Here's a class named A, with a single instance method, m( ), and a single instance variable, v:

public class A { public var v = 10; public function m ...

Get Essential ActionScript 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.