Generics and Comparators Flashcards

1
Q

What are the 4 different types of access levels for class methods or member variables?

A

public

protected

no modifier

private

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

Who can access public methods or member variables?

A
  1. The Class
  2. The SubClasses.
  3. The package.
  4. The World
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Who can access the protected methods or member variables?

A
  1. The Class.
  2. The Subclasses
  3. The Package
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Who can access the private methods or member variables

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

What term refers to things such as public, private, and protected?

A

Modifier

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

What is a modifier give 4 examples.

A

A modifier is a declaration of the access level.

Ex: public, private, protected, no modifier

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

When no modifier has been declared who does the default modifier allow to access member variables or methods?

A
  1. The class.

2. Classes within the package.

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

Give an example of a common generic class that we use all the time.

A

ArrayList

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

What is a generic placeholder?

A

During the class implementation you can use to create a generic placeholder that will be filled in during construction.

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

How do generic types treat class inheritance?

A

They ignore it.

Ex. An ArrayList is NOT an ArrayList

Even though a Triangle is a Shape.

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

How do method signatures apply to generic placeholders?

A

The method signature must specify a type and the method cannot be called on the derived classes of that type.

Ex:
public void doStuff(ArrayList x){….}
ArrayList triList ;
ArrayList shapeList;

doStuff(triList) is ILLEGAL
doStuff(shapeList) is Allowed.

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

How can we create method signatures that will allow a generic type and any of its subclasses to be passed as parameters?

A

Use wildcards
public void doStuff(ArrayList x)
ArrayList triList ;
ArrayList shapeList;

doStuff(triList) is Allowed
doStuff(shapeList) is Allowed.

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

What is an upper-bounded wildcard?

A

extends Shape>
Refers to Shape and any of it’s derived classes.
Ex: Shape, Circle, Triangle

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

What is a lower-bounded wildcard?

A

super Triangle>
Refers to Triangle and any that is a superclass of it.
Ex: Triangle, and Shape.

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

If List and List do not have List as their parent what is their parent?

A

List> is the parent.

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

Why use generics instead of Objects?

A

Generics allow for type-checking at compile time before running. Objects can only be checked at runtime.

Generics allow you to detect type mismatches before your code runs.

17
Q

Give an example of the runtime error if an arrayList took only objects vs using generics.

A

Runtime crash.
ArrayListL; // implemented with Object in mind
L.add(new String (“hi”));
Shape i= (Shape)L.get(0); // crash!

Compile Time error
ArrayList L;
L.add(new String (“hi”));
Shape i= L.get(0); // compile error

18
Q

Why are compile time errors preferred over run time errors?

A

Easier to detect and fix. Won’t run without them fixed so you can’t get false positives.

19
Q

What data types do generics work with?

A

Only with reference types not with primitives,

20
Q

What are 4 important Java primitives and their associated wrapper classes?

A

int Integer
float Float
double Double
char Character

21
Q

What does a wrapper class do?

A

Converts a primitive into an object.

22
Q

Why is the wrapper classes function important?

A
  1. The java.util package handles only objects contains Array, Random, LinkedList etc.
  2. Data structures in the collection framework, such as ArrayList, handle only objects.
  3. Objects are needed to modify the arguments passed into a method. Primitive types are only passed by value. Remember that this means a primitive type is copied for the method call thus it cannot be altered by a void method. Objects are passed by reference.
23
Q

What does Java do automatically for us with wrapper classes?

A
It converts between them automatically when dealing with list accesses.
L.add(5); is equivalent to...
L.add(new Integer(5));

int i = L.get(n); is equivalent to…
int i = L.get(n).intValue();

24
Q

How do you have to declare a generic static method?

A

You must declare the generic type before the return type.

public static boolean doWork(…)

25
Q

Give an example of a generic contains method that searches a generic array for a generic item and returns true if we find it.

A

public static boolean contains(T[] arr, T item){

for(i=0;i

26
Q

What are the two tools we can use to make decisions about sorting generic types?

A
  1. Comparable interface - natural ordering a.compareTo(b)

2. Comparator interface - custom ordering mechanism

27
Q

What interface does compareTo belong to? What does a call to compareTo look like? What does compareTo Return?

A

x.compareTo(y) refers to the natural ordering or Comparable interface.
Returns a negative integer for xy.

28
Q

What interface does compare belong to? How is it different than compareTo? How is it the same?

A

Compare belongs to Comparator for custom ordering or objects that might not have obvious natural ordering.

The call is different as it takes both arguments. comparator.compare(x,y).

The return type is the same:
+int for x>y,
-int for x

29
Q

What is a functor?

A
A class with a single method.
Ex: Comparator.
30
Q

When should you use Comparator vs Comparable?

A

Comparator provides a way for custom comparison logic when you have no control over the types you’re comparing

Comparable provides a way for comparison logic for your own types. However, you must implement the logic in your type class.