Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet76/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   72   73   74   75   76   77   78   79   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Is the code below valid? Explain.
@FunctionalInterface


204.
a.
public interface Interface1 {
public void doSomething();
}
public interface Interface2 extends Interface1{
public void doSomethingElse();
}
Answer:
The code above declares a functional interface called
Interface1 with an abstract method called doSomething. It
then defines another functional interface called Interface2 that
extends Interface1 and has and abstract method called
doSomethingElse. This code is perfectly valid. Interface2
now has two abstract methods, doSomething() and
doSomethingElse(). Since it is not marked with the
@FunctionalInterface annotation, it is okay to have multiple
abstract methods. Had the @FunctionalInterface annotation
been used on Interface2, the code would have caused a
compilation error.
Name some pre–Java 8 interfaces that are made
functional interfaces by Java 8.
Answer:
Prior to Java 8, there are some interfaces that already had a
single abstract method. Java 8 has designated these interfaces
as functional interfaces by specifying the
@FunctionalInterface annotation on them. So, the abstract
methods in these interfaces can be implemented via lambda
expressions. These interfaces are as follows:
java.lang.Runnable: Used to create a Thread


b.
c.
d.
205.
java.util.cocurrent.Callable: Used to create a Thread that
returns a value
java.util.Comparator: Used to compare values
java.io.FileFilter: Used to filter Files
Why are primitive specializations of the in–built functional
interfaces like IntSupplier, BooleanSupplier, etc. added by

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   72   73   74   75   76   77   78   79   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling