Next: 6.11.3 Minor class features Up: 6.11 Classes Previous: 6.11.1 Parameterised classes Contents

##
6.11.2 Inheritance

Classes can inherit from a super-class. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more = class Pasta_plain {` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `macaroni = "tubes";` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `spaghetti = "long and thin";` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `lasagne = "fairly large sheets";` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `}` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Here the new class `Pasta_more` inherits members from the previous class `Pasta_plain`. It also overrides the definition of `lasagne` from `Pasta_plain` with a new value. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.macaroni == "tubes"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.fusilli == "sort of twisty"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.lasagne == "fairly large sheets"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

You can use `this` and `super` to refer to other members up and down the class hierarchy. `super` is the class instance that the current class inherits from (if there's no super-class, `super` has the value `[]`), and `this` is the most-enclosing class instance.

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.super == Pasta_plain` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.this == Pasta_more` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.super.this == Pasta_more` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

therefore:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.lasagne == "fairly large sheets"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.super.lasagne == "large sheets"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Pasta_more.super.this.lasagne ==` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `"fairly large sheets"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

There's a special symbol `root` which encloses all symbols. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `fred = 12;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Freddage = class {` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `fred = 42;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `mystery = root.fred;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `}` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Now `Fred.mystery` will have the value 12.

There's another special symbol called `scope` which encloses all symbols in the file this definition was loaded from. If you want to refer to another definition in the same file which is being masked somehow, use `scope`.

You can use the built in function `is_instanceof` to test whether an instance is or inherits from a class. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `is_instanceof "Pasta_more" Pasta_more == true` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `is_instanceof "Pasta_plain" Pasta_more == true` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `is_instanceof "Pasta_more" Pasta_plain ==` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `false` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

The super-class constructor can take arguments, and these arguments can refer to class members. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `Fresh_pasta pasta_name = class` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `My_pasta pasta_name cooked {` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> |  |  |  | `cooked = 2;` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `}` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Defines a class for fresh pasta, which always cooks in 2 minutes. You need to be careful not to make loops: if `cooked` did tried to refer to something in the super-class, this class would never construct properly. `nip2` unfortunately does not check for this error.

Finally, the superclass can be a fully constructed class. In this case, the superclass is cloned and the new class members wrapped around it. You can use this to write a class which can wrap any other class and add members to it. Many of the toolkit menu items use this trick to enable them to work for any object type.

Next: 6.11.3 Minor class features Up: 6.11 Classes Previous: 6.11.1 Parameterised classes Contents