generics ch14 Flashcards

1
Q

what is generics

A

java allows class and method definitions that include parameters for types - these definitions are called generics

Generic programming with a type parameter enables code to be written that applies to any class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

copies of arrayList

A

deep copy is needed - clone method not good enough - it produces a shallow copy

In order to make a deep copy, it must be possible to make a deep copy of objects of the base type
Then a deep copy of each element in the ArrayList can be created and placed into a new ArrayList object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

the arraylist class is a parameterized class - what does this mean

A

It has a parameter, denoted by Base_Type, that can be replaced by any reference type to obtain a class for ArrayLists with the specified base type

class that needs another class as a parameter - is like a cookie cutter class - have type parameters - e.g. type Employee is the parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

generic constructors

A

public Class()

A constructor can use the type parameter as the type for a parameter of the constructor, but in this case, the angular brackets are not used
public Pair(T first, T second)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

instantiated generic class

A

Pair pair = new Pair(“Happy”, “Day”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

can you use primitive types in generic class parameters

A

no - The type plugged in for a type parameter must always be a reference type
reference types include arrays
but now that Java has auto boxing - not a big restriction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what are the places where an ordinary class name would be allowed but the type parameter wouldnt be allowed

A
In particular, the type parameter cannot be used in simple expressions using new to create a new object
For instance, the type parameter cannot be used as a constructor name or like a constructor:
T object = new T();
T[] a = new T[10];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

is the instantiation of a generic class be an array base type?

A

no
Arrays such as the following are illegal:
Pair[] a = new Pair[10];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

generic class with multiple type parameters

A

TwoTypePair

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what are the bounds for type parameters

A
  • may be on a class name rather than an interface name
  • Then only descendent classes of the bounding class may be plugged in for the type parameters

public class Two

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

declaring generic methods

A

public static T genMethod(T[] a)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

invoking generic methods

A

String s = NonG.genMethod(c);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

inheritance with generic classes

A

Given two classes: A and B, and given G: a generic class, there is no relationship between G<a> and G<b></b></a>

This is true regardless of the relationship between class A and B, e.g., if class B is a subclass of class A
</b></a>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly