Week 2 - Interfaces, Downcasting, Polymorphism, Listeners Flashcards

0
Q

Why should one use an interface instead of a class?

A

A class can implement a number of interfaces in Java, whereas they can only extend one parent class. Interfaces allow unrelated classes to be treated the same way without creating a complex and inefficient inheritance tree.

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

What is an interface?

A

An interface is a collection of abstract operations. Entities can implement these interfaces and by doing so inherit their operations. This allows different entities who implement the same interface to be substituted for one another.

In Java, interfaces define methods, and classes implement these interfaces, gaining the interface’s methods, allowing classes and objects that are unrelated in the inheritance hierarchy to be substituted for one another.

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

What is downcasting?

A

Downcasting refers to the casting of a reference of an object of a base class to that of one of its derived classes. I.e. superclass is casted to one of its subclasses.

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

Is downcasting safe to do?

A

It is when the reference is being cast to one of its subclasses.

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

What is polymorphism?

A

Polymorphism refers to the ability to handle different data types using the a uniform interface.

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

What types of polymorphism does Java support?

A

Java supports method overloading, method overriding, and generics (parametric polymorphism).

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

What is method overriding?

A

Method overriding occurs when a child class implements its own version of a previously implemented method in its parent class. This allows a child class to perform the same operation as its parent class but in a different way.

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

What is method overloading?

A

Method overloading occurs when a single method name has multiple headers, each taking a different number and types of arguments. This allows a single method name to handle different parameters.

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

What are Java generics?

A

Generics is an implementation of parametric polymorphism. It allows a class or interfaces to take a type as a parameter so that the class can be made to handle different types without creating a different class or different sets of methods for each different data type.

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

What is the function of a listener in Android?

A

A listener encapsulates code to be executed in response to an event. Listeners are not immutably coupled to events, they in fact allow code to be executed to be decoupled from events.

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

How are Android listeners implemented in Java?

A

A listener is implemented as either a Java interface or an adapter class. They both define methods that are called in response to events.

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

How is a listener typically used to react to user interface interaction?

A

A listener is created either by having a class implement a listener interface or creating a class that extends a listener adapter, then registering that listener with the appropriate view.

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

What is the primary difference between a listener interface and a listener adaptor?

A

Listener interfaces are Java interfaces and consequently allow a class to implement many different listeners, but must implement all of their methods.

A listener adaptor is a Java class and consequently only allows a class to inherit one listener adaptor class (or any other class for that matter). The benefit of using a listener adaptor is being able to only override the method(s) one desires.

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