Test 2 Flashcards
What happens if I declare a specific type for a list interface
declare a specific type → need to define
many List interfaces, one for each specific type
creates a lot of duplicate code
what happens if I declare a list with elements are simply Object
lose the safety of compile-time type checking (need to cast) cant do anything with the list because it needs to be casted to type
type parameter
a variable that takes a type as its value and
is used to make a generic class or interface more specific
what type is List<E></E>
generic type
List<String></String>
parameterized type
List
raw type
what can you do with Parametrized types
Can declare variables of parameterized types
Can construct objects of parameterized types
Can invoke methods on objects of parameterized types
Can define methods that take or return parameterized types
what can’t raw types do
never declare or instantiate!
what can generics extend
Generic types can extend/implement other generic types
We could not define…
public class MyList<T> implements List<E>
Generic types can extend non-generic types</E></T>
what can non generics extend
Non-generic types can also extend/implement generic types
Invariance
must use the exact type specified
Contravariance
can use a less specific type than declared
Covariance
can use a more specific type than declared
Pros of covariance
more flexible
invariance
more safe (less prone to failing at runtime)
boolean add(E elem) (.add)
adds elem to the end of the List and returns true, if its an element it return false
add(int idx, E elem)
adds elem to the List at index idx
E set(int idx, E elem) (.set)
places the element at index idx in the
List, replacing what was there previously
E get(int idx)
returns the element at index idx in the List
int indexOf(E elem)
returns the index of elem in the List
boolean contains(Object obj)
returns true if obj is in the
List; false otherwise
E remove(int idx)
removes element at index idx from the List
boolean remove(Object obj)
removes obj from the List
if remove could be boolean remove or normal remove what does java go with
removes number at that idx
what do u do when u want to remove a certain value from a list but that number is also a valid index
.remove(Integer.valueOf());
If we define a generic type (class or interface) by declaring one or more type
parameters, what is the scope of those type parameter(s)?
The scope would be the entire class or a single method.
There are only two circumstances under which it may be a good design to use a
raw type. What are those circumstances?
a. When you want to access a static field or method of generic class
because it enables you to quickly create non empty lists
b. When you want to use instanceOf to perform type checking
what are javas 3 primary ways of creating a constructor
- Default constructors (zero args) – create an empty container: List<Integer> myList = new ArrayList<Integer>(); 2.Copy constructors – create a shallow copy of a given container 3.Static “factory” methods – quick means of building a new
non-empty container
List<Integer> myList = List.
of(1, 3, 5, 7, 9)</Integer></Integer></Integer>
difference between generic methods and types
Generic methods offer greater flexibility. Generic types offer better consistency & type safety generic types are a good fit for when
you want a consistent type across
multiple methods that share state
If we define a generic method by declaring one or more type parameters, what is
the scope of those type parameter(s)?
The scope of the type parameter is simply the single method for which it is
defined