Skip to main content

more options

The bottom-up rule

The bottom-up rule defines how to find a variable, or a method with a matching signature, in an object:

Bottom-up rule: When looking in an object for a variable, or for a method with a particular signature, start from the bottom of the object and look up upward until it is found (or until it is determined that it is not present).

object with 4 partitionsWe use object a0, to the right, to illustrate. It has four partitions. From it, we see that Square is a subclass of Rectangle, which is a subclass of Shape, which is a subclass of Object. The expression

a0.h

refers to variable h in partition Rectangle, since that is the first (and only) h encountered when searching from the bottom of the object to the top.

The call a0.area() calls the method defined in partition Rectangle, while a0.toString() calls the method in partition Square, since these are the first matching methods encountered when searching from bottom to top.

For methods, the bottom-up rule always gives us the overriding method, if a method is inherited in a subclass.

In this course, we refrain from redefining an inherited variable in a subclass and don't explain what happens in this case. Redefining an inherited variable is rarely done, because there is little use for it. Therefore, for a variable, the bottom-up rule finds the only variable of the given name in the object.