Week 2 - Interfaces, Downcasting, Polymorphism, Listeners Flashcards
Why should one use an interface instead of a class?
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.
What is an interface?
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.
What is downcasting?
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.
Is downcasting safe to do?
It is when the reference is being cast to one of its subclasses.
What is polymorphism?
Polymorphism refers to the ability to handle different data types using the a uniform interface.
What types of polymorphism does Java support?
Java supports method overloading, method overriding, and generics (parametric polymorphism).
What is method overriding?
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.
What is method overloading?
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.
What are Java generics?
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.
What is the function of a listener in Android?
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 are Android listeners implemented in Java?
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 is a listener typically used to react to user interface interaction?
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.
What is the primary difference between a listener interface and a listener adaptor?
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.