Inheritance and Lookup

3: super

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

Goal

What is super?

Take 5 min and write the definition of super

Challenge Yourself With super!

    A new bar
    > ...
    B new bar
    > ...
    C new bar
    > ...

Challenge Yourself With super!

    A new bar
    > 10
    B new bar
    > 20
    C new bar
    > 100

super Changes Where the Lookup Starts

Evaluation of aC bar

  1. aC's class is C
  2. no method bar in C
  3. look up in B - bar is found
  4. method bar is executed
  5. bar is sent to super
  6. super is aC but lookup starts in A
  7. bar is found in A and executed
  8. foo is sent to aC
  9. foo is found in C

super Changes Where the Lookup Starts

self is Dynamic

We don't know which foo method bar refers to

super is Static

  • at compilation-time, we know that B>>foo refers to A>>foo
  • we should look above the class containing the method using super

Even Some Books Got it Wrong

In reality it does not loop, the definition is wrong

What You Should Know

/