#5 inheritance (2.1 +2.2) Flashcards
(15 cards)
override
a subclass can override (redefine) methods in the superclass same name and signature
(uses super() to call the original method)
dynamic dispatch/late binding/dynamic method binding
overriding, happens during program execution
nonstatic: how is overriding determined
the type of object (not the reference) determines which method will be called
static: how is overriding determined
the type of the variable determines which method will be called
overriding and privacy
subclasses can make overridden methods more private. they cannot make them more public.
private things in a superclass
subclass cannot directly access private fields and methods in its superclass(es) CAN THET BE OVERRIDEN? no
protected
subclasses have access (same package or not)
subclasses and super classes can have fields with the same name how
you use super. to differentiate between the two. subclass takes precedence. super. only refers to the immediate super class
final with overriding
final methods cannot be overridden (nor can private, b/c they are implicitly final)
final with inheritance
final:
cannot be extended (have subclasses)
it’s methods are implicitly final
(string is a java library final class)
what can a super class access?
only fields and methods in its class (doesn’t know subclass exists)
upcasting
cast a subclass reference to a superclass reference
downcasting
cast a superclass reference to a subclass reference
cast a car to a vehicle
ALWAYS VALID
cast a vehicle to a car
ONLY IF VEHICLE IS ACTUALLY OF TYPE CAR!