You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When run, SLList created & address stored in someList variable
String "elk" inserted into SLList referred to by addFirst
List61B object will access/use implemented methods from corresponding subclass
Can be set to new SLList<>() as well, String not necessary
Default Methods & For Loops
Implementation Inheritance
Use default keyword to specify method that subclasses should inherit from interface
default public void print() {}
default method of interface can't access instance variables (b/c no instance variables in interface), have to use methods already specified in interface to implement
Can override default methods
Dynamic Method Selection
Static Type vs Dynamic Type
Every variable in Java has "compile-time type"/"static type"
Type specified at declaration, never changes
Variables also have "run-time type"/"dynamic type"
Type specified at instantiation (e.g. when using new)
Equal to type of object being pointed at
publicstaticvoidmain(String[] args) {
LivingThinglt1; // Static = LivingThing, Dynamic = nulllt1 = newFox(); // Static = LivingThing, Dynamic = Fox// Dynamic type of a1 set to same Dynamic type as lt1Animala1 = lt1; // Static = Animal, Dynamic = Fox// Dynamic type of h1 set to same Dynamic type as a1Foxh1 = a1; // Static = Fox, Dynamic = Foxlt1 = newSquid(); // Static = LivingThing, Dynamic = Squid
}
Variable
Static Type
Dynamic Type
lt1
LivingThing
Squid
a1
Animal
Fox
h1
Fox
Fox
When calling method of object using variable with:
Compile-time type X
Run-time type Y
If Yoverrides method, Y's method used instead
Known as "dynamic method selection"
Overloaded Methods
When overloading, dynamic method selection not used → static type used instead