Generics Flashcards

1
Q

Can you explain the problem with using Object as a reference type in Java?

A

When we use Object as a reference type in Java, we lose the ability to use any methods or fields specific to the actual type of the object being referenced. This means that we cannot ensure the existence of specific operations or enforce any type constraints, as we can only access the methods and fields defined in the Object class.

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

What is the issue with creating generic code using Object as the reference type?

A

The issue with using Object as the reference type for generic code is that it leads to unchecked type casting and can result in runtime errors. This is because we must cast the Object reference to the actual type of the object in order to use any methods or fields specific to that type. If we cast to the wrong type, or the object being referenced is not of the expected type, we will encounter a ClassCastException at runtime.

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

What is a better solution to using Object ?

A

A better solution is to use interfaces to define the expected behavior of objects, and then use these interfaces as the reference type for generic code. This allows us to ensure the existence of specific operations and enforce type constraints, while still being able to create generic code that can work with any object that implements the required interface.

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

What is the issue with using Product as a basis for organising products into lists?

A

While using Product (and its subclasses) works well for organising products into lists, it does not work for anything outside of the Product hierarchy. This is because the operations used to organise products don’t necessarily have anything to do with the Product class.

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

What is the problem with using List of Objects as an ADT?

A

Using List of Objects as an ADT can lead to issues with determining the type of object stored within the list, making it difficult to perform operations and leading to errors.

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

How can we determine the type of an Object in a List of Objects?

A

We can determine the type of an Object in a List of Objects using the “instanceof” keyword, which checks if the object is an instance of a specific class or interface.

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

Why is using instanceof not an ideal solution to the problem with List of Objects?

A

Using “instanceof” is not an ideal solution because it can lead to lots of cases to handle and make the code difficult to maintain as more classes and interfaces are added.

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

How can using List of Objects lead to lots of cases to handle?

A

Using List of Objects can lead to lots of cases to handle because we may need to check the type of object before performing an operation on it, leading to lots of conditional statements and making the code difficult to read and maintain.

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

What is the disadvantage of having no idea what type of object is stored in a List of Objects?

A

The disadvantage of having no idea what type of object is stored in a List of Objects is that we may not be able to perform operations on it, and we may need to check the type of object before performing any operations, which can lead to lots of cases to handle and make the code difficult to read and maintain.

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

What is the benefit of using generics?

A

Generics allow us to specify a type parameter, which puts restrictions on the type that can be put into a list or other data structure. This helps to ensure type consistency without the need for reimplementation.

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

How does specifying a type parameter constrain the use of a list or other data structure?

A

By specifying the type parameter in the outside world, we constrain how the list can be used. This means that only certain types of objects can be added to the list, ensuring type consistency.

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

Why is it important to have type consistency in a list or other data structure?

A

Having type consistency helps to prevent errors and improve code quality. It also makes the code easier to understand and maintain, as developers can rely on the types of objects stored in the list.

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

How does using generics avoid the need for reimplementation?

A

Without generics, it can be difficult to ensure type consistency when working with lists or other data structures. This often requires developers to reimplement the same code for different types of objects. By using generics, the type parameter can be specified once, and the same code can be used for multiple types of objects.

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

What are some potential drawbacks of using generics?

A

Generics can sometimes add complexity to code, and may be difficult for new developers to understand. Additionally, using generics can result in longer compile times and larger executable file sizes.

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

What are generics in Java?

A

Generics in Java are a way to create type-safe code that can work with multiple types of data.

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

How can generics be used locally for one method?

A

Generics can be defined for a single method by declaring the type parameter before the return type of the method.

17
Q

What is the benefit of using generics for a single method?

A

Using generics for a single method allows for greater type safety and code reusability. It allows the method to work with multiple types of data without having to be redefined for each type.

18
Q

When would it be useful to use generics for a static method?

A

It would be useful to use generics for a static method when the method needs to be called without an instance of the class, or when the method does not depend on any state of the class.

19
Q

How can a generic method be called with a specific type?

A

A generic method can be called with a specific type by specifying the type in angle brackets before the method name when it is called. For example, if a method is defined as
public static <T> void printArray(T[] arr),
it can be called with an array of integers like this:
printArray(new Integer[]{1,2,3}).</T>

20
Q

What is the syntax for defining a method with a generic type parameter?

A

The syntax for defining a method with a generic type parameter is to put the type parameter in angle brackets before the return type, like this: public static <T> void methodName(T param1, T param2). The type parameter can be any valid Java identifier.</T>

21
Q

What is the limitation of using the “new” keyword within a generic?

A

Within a generic, you cannot instantiate the type variable.

22
Q

What is the limitation with instantiating type variables within a generic?

A

The limitation is that you cannot instantiate the type variable within a generic. This means that you cannot create a new object of that type within the generic method or class.

23
Q

What is the workaround for creating objects in a generic?

A

The workaround is to construct the objects in the outside world and then pass the references to the generic method or class.

24
Q

Why can’t simple types be used as type parameters in generics?

A

Simple types, such as int or double, cannot be used as type parameters in generics because generics only allow class types to be used as type parameters.

25
Q

How does Java enforce type consistency with generics?

A

Java enforces type consistency with generics by using template parameters. Template parameters are placeholders for the actual types that will be used in the generic class or method. When the generic is instantiated with a specific type, the template parameter is replaced with that type, and Java ensures that all operations on the generic object are type-safe.