Interfaces Flashcards

1
Q

Interfaces are IMPLEMENTED

A

*can contain:
- abstract methods(does not contain an implementation)
- default methods
- static methods.
- constant declarations. All constant values defined in an interface are implicitly public, static, and final. ( you can omit these modifiers)
** All members are public
**If you need to add methods to original interfaces, then programmers who have implemented those interfaces would have to rewrite their implementations. If you add them as static methods, then programmers would regard them as utility methods, not as essential, core methods.
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.In particular, default methods enable you to add methods that accept lambda expressions as parameters to existing interfaces.
**Users who have classes that implement interfaces enhanced with new default or static methods do not have to modify or recompile them to accommodate the additional methods!!!!!
**In addition to default methods, you can define static methods in interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.) This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class.

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

An interface can EXTEND MULTIPLE interfaces

A

Interfaces can extend from other interfaces.
A sub-interface can include a method signature that matches a parent interfaces method without causing ambiguity.
Having ambiguous fields or methods does not cause any problem by itself (except in the case of default methods) but referring to such fields or methods in an ambiguous way will cause a compile time error???

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

Iterator

A

java.util
Interface Iterator<E></E>

Methods:
*forEachRemaining(Consumer<? super E> action)
Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

*hasNext()
Returns true if the iteration has more elements.

*next()
Returns the next element in the iteration.

*
remove()
Removes from the underlying collection the last element returned by this iterator (optional operation).

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

Ambigous filed and methods defined in multiple interfaces

A

Having ambiguous fields does not cause any problems but referring to such fields in an ambiguous way will cause a compile time error. Problem solved with casting:
EG:
class TestClass implements I1, I2{
public void m1() { System.out.println(“Hello”); }
public static void main(String[] args){
TestClass tc = new TestClass();
( (I1) tc).m1();
}
}
interface I1{
int VALUE = 1;
void m1();
}
interface I2{
int VALUE = 2;
void m1();
}

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

Extending interfaces

A

When you extend an interface that contains a default method, you can do the following:
-Not mention the default method at all, which lets your extended interface inherit the default method.
-Redeclare the default method, which makes it abstract.
-Redefine the default method, which overrides it.

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

INTERFACES

A

Ideally, interfaces shouldn’t encapsulate behavior, and we should only use them for defining the public API of a certain type.

When it comes to maintaining backward compatibility with existing code, however, static and default methods are a good trade-off.(to be able to add functionality without breaking the classes that implement the interfaces )

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

Functional Interfaces

A

!n interface that contains only one abstract method. They can have only one functionality to exhibit!!!!!!It is a feature in Java, which helps to achieve functional programming approach!!
Functional Interfaces introduced in Java 8 allow us to use a lambda expression to initiate the interface’s method and avoid using lengthy codes for the anonymous class implementation.

Functional interfaces are used and executed by representing the interface with an annotation called @FunctionalInterface.

However, they can include any quantity of default and static methods!!!Runnable, ActionListener, Comparable are some of the examples of functional interfaces.

Java provides predefined functional interfaces to deal with functional programming by using lambda and method references in the java.util.function package.

Java SE 8 included four main kinds of functional interfaces
Consumer
Predicate
Function
Supplier

Amidst the previous four interfaces, the first three interfaces,i.e., Consumer, Predicate, and Function, likewise have additions that are provided beneath:

*****Consumer - takes arguments and returns nothing!!!
**BiConsumer<T,U> It represents an operation that accepts two input arguments and returns no result. (It is used in iterating through the entries of the map. ????)
**Consumer<T> It represents an operation that accepts a single argument and returns no result.
EG: Consumer<Integer> consumer = (value) -> System.out.println(value);
**DoubleConsumer It represents an operation that accepts a single double type argument and returns no result.
**IntConsumer It represents an operation that accepts a single integer argument and returns no result.
**LongConsumer It represents an operation that accepts a single long type argument and returns no result.
**ObjDoubleConsumer<T> It represents an operation that accepts an object and a double argument, and returns no result.
**ObjIntConsumer<T> It represents an operation that accepts an object and an integer argument. It does not return result.
**ObjLongConsumer<T> It represents an operation that accepts an object and a long argument, it returns no result.</T></T></T></Integer></T>

****Predicate–>a type of function which accepts arguments and does some sort of processing on it, and returns a boolean (True/ False) answer(encapsulates the logic of filtering )
EG: Predicate predicate = (value) -> value != null;
**Predicate<T> It represents a predicate (boolean-valued function) of one argument.
**BiPredicate<T,U> It represents a predicate (boolean-valued function) of two arguments.
**DoublePredicate It represents a predicate (boolean-valued function) of one double type argument.
**IntPredicate It represents a predicate (boolean-valued function) of one integer argument.
**LongPredicate It represents a predicate (boolean-valued function) of one long type argument.</T>

**Function: receives arguments and returns a value after the required processing.
**Function<T,R> It represents a function that accepts one argument and returns a result.
**BiFunction<T,U,R> It represents a function that accepts two arguments and returns a a result.
**DoubleFunction<R> It represents a function that accepts a double type argument and produces a result.
**DoubleToIntFunction It represents a function that accepts a double type argument and produces an int type result.
**DoubleToLongFunction It represents a function that accepts a double type argument and produces a long type result.
**IntFunction<R> It represents a function that accepts an integer argument and returns a result.
**IntToDoubleFunction It represents a function that accepts an integer argument and returns a double.
**IntToLongFunction It represents a function that accepts an integer argument and returns a long.
**LongFunction<R> It represents a function that accepts a long type argument and returns a result.
**LongToDoubleFunction It represents a function that accepts a long type argument and returns a result of double type.
**LongToIntFunction It represents a function that accepts a long type argument and returns an integer result.
**ToDoubleBiFunction<T,U> It represents a function that accepts two arguments and produces a double type result.
**ToDoubleFunction<T> It represents a function that returns a double type result.
**ToIntBiFunction<T,U> It represents a function that accepts two arguments and returns an integer.
**ToIntFunction<T> It represents a function that returns an integer.
**ToLongBiFunction<T,U> It represents a function that accepts two arguments and returns a result of long type.
**ToLongFunction<T> It represents a function that returns a result of long type.</T></T></T></R></R></R>

***Unary Operator and Binary Operator: extend the Function and Bi-Function, both the input and output values must be identical and of the same type.
**BinaryOperator<T> It represents an operation upon two operands of the same data type. It returns a result of the same type as the operands.
**DoubleBinaryOperator It represents an operation upon two double type operands and returns a double type value.
**DoubleUnaryOperator It represents an operation on a single double type operand that produces a double type result.
**IntBinaryOperator It represents an operation upon two int type operands and returns an int type result.
**UnaryOperator<T> It represents an operation on a single operand that returnsa a result of the same type as its operand.
**LongUnaryOperator It represents an operation on a single long type operand that returns a long type result.
**IntUnaryOperator It represents an operation on a single integer operand that produces an integer result.
**LongBinaryOperator It represents an operation upon two long type operands and returns a long type result.</T></T>

*******Supplier: does not take any input or argument and yet returns a single output(used in the lazy generation of values)
**Supplier<T> It represents a supplier of results.
**BooleanSupplier It represents a supplier of boolean-valued results.
**DoubleSupplier It represents a supplier of double type results.
**IntSupplier It represents a supplier of integer type.
**LongSupplier It represents a supplier of long type results.</T>

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

Functional Interfaces

A

Interface that contains only one abstract method. Used with lambda expressions as a functional way of programming without using anonymous inner classes!!

**Consumer –> takes arguments and returns nothing
***Predicate –> takes arguments and returns a boolean
**
*Function –> takes arguments and returns a value
**
Supplier: –> does not take arguments but returns an output

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