overriding
also called shadowing
■ Overriding
is defined as when a subclass method provides three things which are the same as in the overridden method:
(1) method name, and
(2) parameter list, and
(3) return type. See signature.
■ You cannot
change the return type of an overridden method.
■ You cannot make the overriding method be less
accessible (meaning more private)
than the overridden method. See modifiers.
■ The order of this accessibility, from least
restrictive to most restrictive, is:
(least restrictive is) public, then
protected,
then
friendly/package/default,
and lastly
(most restrictive is) private
■ You cannot override a public or protected
method using no modifiers (which would be assigning friendly/package, which is more restrictive). i.e.
■ You cannot
override a private
method either, in any way. (It's private. You cannot override it because
it cannot be seen outside its class.) Doing so compiles but becomes overloading
in all cases.
■ When
overriding you cannot throw higher level exceptions or exceptions any
from different hierarchies. You
can just throw none, the same, fewer, or subclasses
of those exceptions already being thrown in the overridden method.
■ You cannot
ever override a non-static
method with a static
method, or a static
method with a non-static
method.
■ super.methodname( ) calls parent methods
which you did not override.
■ If an
overridden method is synchronized,
you can safely ignore the synchronized.
■ It is not
overriding if, in subclasses, you simply re-use the names of higher private methods again. That is allowed.
No comments:
Post a Comment