Let me just reiterate that this is probably the hardest topic in the course to really wrap your head around---so if it seems confusing, don't worry too much.
| Binding | ||||||||||||||
| Binding is the process Java uses to connect a method name with the actual code
that implements it, or a variable name with the particular memory slot that
contains it---which is tricky when several methods or variables have the same
name. Binding starts searching for a method or variable at a specific
starting class. Then it looks for the version of the item that is most
specific to that class. (So if the starting class does not
contain a version, it will go to the most immediate parent class that does
contain one).
Static Binding
Dynamic Binding
Note that some version of the method or variable still has to be accessible via
plain static binding. This guarantees that when dynamic binding happens
at runtime, there will always be something to find. So a variable
can never point to an object whose class doesn't have any version of the
item in question. Comparison
|
||||||||||||||
| Interfaces | ||||||||||||||
| Because of dynamic binding, it's ok to have a variable where the reference type
is an interface. Since it is impossible to instantiate an object that is
actually of that type, we know the reference will instead always be pointing to
an object of a type that implements the interface. Dynamic binding
will be used based on this object's type. Notice how all items that do
use static binding cannot be placed in interfaces--think about why it would be
a problem if they could be...
Interfaces can extend other interfaces. Classes can only implement
interfaces; they cannot extend them.
|
||||||||||||||
| Casting | ||||||||||||||
| Casting is the process of changing from one reference type to
another. The type of the object being pointed to is not changed.
In fact, nothing about the object being pointed to changes, and nothing about
the original pointer itself changes. Casting gives you a new pointer, of
a different type, to the same object.
Upcasting - Casting from a child type to its parent type--more general.
An explicit cast looks like: (type to cast to)reference Example: intPuzzle = (PuzzleAsInt)somePuzzle Becuase of Java's "order of operators" (precedence), you will often need
to add extra parentheses:
|
||||||||||||||