Ok, so we didn't cover every nitty gritty little detail of this stuff in section. If you understand the key points we touched on, you'll do fine on whatever test question(s) we ask about polymorphism. Hopefully this can act as a solid study reference without being too overwhelming. And if you want to learn all the finer points, they're here too.
See also: Lecture
Notes for this topic (week 9).
Constructors & Initializers | ||||||||||||||
![]() |
Starting at the constructor that is actually invoked with
new, things happen in this order:
|
|||||||||||||
Access Modifiers | ||||||||||||||
A child class method or variable can be made more public than
the one it is overriding/shadowing, but not less public.
|
||||||||||||||
Binding | ||||||||||||||
Needed when there is a name conflict between one method/variable
and another inherited from a parent class. The argument types for
methods need to be identical as well as the name or there will be no conflict.
Binding decides which of the conflicting items to use. Both methods
of binding in Java start from a particular type, and then look for the
version of the item that is most specific to that type. (So
if the given type does not contain a version, it will use the most immediate
parent type 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 choose. That is, the variable can never point to an object type that doesn't have any version of the item in question--because, then what the heck would you pick? Comparison
Binding and this
For example:
|
||||||||||||||
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 would 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; only the pointer
itself changes.
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:
|