Hi Friends,
In this post, I'm sharing interview questions asked on Java 8.
You can also read other interview questions-answers from my other interviews:
Java 8 Interview Questions:
Question 1:
How to use Default method in java 8?
Answer:
Java 8 introduced default or "Defender methods" as a new feature which allows developers add new methods definitions in interface without breaking existing functionalities/implementation of these interfaces.
Lets take an example for understanding "How to use it":
public interface OldInterface{
public void existingMethod();
default public void newDefaultMethod(){
System.out.println("Code goes here");
}
}
public class OldImplementation implements OldInterface{
public void existingMethod(){
}
}
Now create instance of OldImplementation instance and call default method as:
OldImplemenattaion olI = new OldImplementation();
olI.newDefaultMethod();
Example of Default method added in Java JDK is forEach() which is added in Iterable and Stream interfaces as:
public interface Iterable<T>{
default public void forEach(Consumer<? super T> action){
for(T t : this)
action.accept(t);
}
}
Question 2:
Define Multiple inheritance ambiguity problem in Default methods.
Answer:
Since java class can implement multiple interfaces and each interface can define a default method with same method signature , therefore the inherited methods can conflict with each other.
Let's understand it through an example:
public interface InterfaceA{
public void default method(){
}
}
public interface InterfaceB{
public void default method(){
}
}
public class Implementation implements InterfaceA, InterfaceB {
}
The above code will fail to compile.
In order to fix this, we need to provide default method implementation as:
public class Implementation implements InterfaceA, InterfaceB {
public void default method(){
}
}
If we want to invoke default implementation provided by any of the super Interface, then we can do that as:
public class Implementation implements InterfaceA, InterfaceB {
public void default method(){
Interface.super.method();
}
}
Note: We can choose any default implementation or both as part of our new method.
Question 3:
What other method has been introduced in interface in Java 8?
Answer:
In java 8 , static method has also been introduced. These are called by interface name.
They are introduced to increase the degree of cohesion of a design by putting together all related methods in one single place without having to create an object.
For example, see below code:
public interface Vehicle{
static int getPower(){
return 0;
}
}
Call it as:
Vehicle.getPower();
Question 4:
What is the difference between default method and regular method?
Answer:
Different between default method and regular method are as follows:
Question 5:
How Lambda is useful in java?
Answer:
Lambda is a type of functional interface. A functional interface is an interface that has exact one abstract method. e.g. : Runnable, Callable, ActionListener, Comparator etc.
Lets' take an example to understand how Lambdas are useful in java:
@FunctionalInterface
public interface ITrade{
public boolean check(Trade t);
}
So, above interface has only one abstract method that takes a Trade object.
Lets create a Lambda expression for this interface:
ITrade newTradeChecker = (t) -> t.getStatus().equals("new");
The real power of Lambdas come when we start creating a multitude of them representing real world behavioral functions as:
ITrade bigLambda = (t) -> t.getQuantity() > 13000;
ITrade smallLambda = (t) -> t.getStatus() < 10000;
Now, we have a class filterTrades that will filter trades based on which Lambda we have passed for filetering.
private List<Trade> filterTrades(ITrade trade, List<Trade> trades){
List<Trade> newTrades = new ArrayList<>();
for(Trade t : trades){
if(trade.check(t)){
newTrade.add(t);
}
}
}
So, in this way, it will behave differently based on Lambda passed to filterTrades() method.
Question 6:
What are the differences between Lambda and anonymous class?
Answer:
Below is the list of differences between Lambda and anonymous class:
Question 7:
What are the types of Lambdas?
Answer:
There are two types of Lambdas in Java 8:
Non-capturing Lambdas: These lambdas only use fields inside their bodies, as:
public class NonCapturingLambda{
public static void main(String[] args){
Runnable nonCapLambda = () -> System.put.println("NonCapturingLambda");
nonCapLambda.run();
}
}
Capturing Lambdas: These lambdas access fields outside their bodies, as:
public class CapturingLambda{
public static void main(String[] args){
String str = "CapturingLambda";
Runnable capLambda = () -> System.put.println("CapturingLambda = "+str);
capLambda.run();
}
}
That's all for this post. I'll share more questions in Part - II.
Thanks for reading.
In this post, I'm sharing interview questions asked on Java 8.
You can also read other interview questions-answers from my other interviews:
- Java Interview @ Aricent - Part - 2
- Java Interview @ Gemini Solutions
- Java Interview @ OLX
- Java Interview @ SAP
- Java Interview @ GalaxE
- Java Interview @ OrangeMantra
- Java Interview @ Concirrus
- Java Interview @ NexGen Solutions
- Java Interview @ Capgemini
- Java Interview @ Dew Solutions
- Java Interview @ Birdeye
- Java Interview @ RBS
Java 8 Interview Questions:
Question 1:
How to use Default method in java 8?
Answer:
Java 8 introduced default or "Defender methods" as a new feature which allows developers add new methods definitions in interface without breaking existing functionalities/implementation of these interfaces.
Lets take an example for understanding "How to use it":
public interface OldInterface{
public void existingMethod();
default public void newDefaultMethod(){
System.out.println("Code goes here");
}
}
public class OldImplementation implements OldInterface{
public void existingMethod(){
}
}
Now create instance of OldImplementation instance and call default method as:
OldImplemenattaion olI = new OldImplementation();
olI.newDefaultMethod();
Example of Default method added in Java JDK is forEach() which is added in Iterable and Stream interfaces as:
public interface Iterable<T>{
default public void forEach(Consumer<? super T> action){
for(T t : this)
action.accept(t);
}
}
Question 2:
Define Multiple inheritance ambiguity problem in Default methods.
Answer:
Since java class can implement multiple interfaces and each interface can define a default method with same method signature , therefore the inherited methods can conflict with each other.
Let's understand it through an example:
public interface InterfaceA{
public void default method(){
}
}
public interface InterfaceB{
public void default method(){
}
}
public class Implementation implements InterfaceA, InterfaceB {
}
The above code will fail to compile.
In order to fix this, we need to provide default method implementation as:
public class Implementation implements InterfaceA, InterfaceB {
public void default method(){
}
}
If we want to invoke default implementation provided by any of the super Interface, then we can do that as:
public class Implementation implements InterfaceA, InterfaceB {
public void default method(){
Interface.super.method();
}
}
Note: We can choose any default implementation or both as part of our new method.
Question 3:
What other method has been introduced in interface in Java 8?
Answer:
In java 8 , static method has also been introduced. These are called by interface name.
They are introduced to increase the degree of cohesion of a design by putting together all related methods in one single place without having to create an object.
For example, see below code:
public interface Vehicle{
static int getPower(){
return 0;
}
}
Call it as:
Vehicle.getPower();
Question 4:
What is the difference between default method and regular method?
Answer:
Different between default method and regular method are as follows:
- Default method comes with default keyword by default. Regular method doesn't use any such keyword.
- Default method is defined in interface and an interface doesn't have any state, so default method cannot change the state of the object. While regular method can change the state of the object by changing value of it's parameters.
Question 5:
How Lambda is useful in java?
Answer:
Lambda is a type of functional interface. A functional interface is an interface that has exact one abstract method. e.g. : Runnable, Callable, ActionListener, Comparator etc.
Lets' take an example to understand how Lambdas are useful in java:
@FunctionalInterface
public interface ITrade{
public boolean check(Trade t);
}
So, above interface has only one abstract method that takes a Trade object.
Lets create a Lambda expression for this interface:
ITrade newTradeChecker = (t) -> t.getStatus().equals("new");
The real power of Lambdas come when we start creating a multitude of them representing real world behavioral functions as:
ITrade bigLambda = (t) -> t.getQuantity() > 13000;
ITrade smallLambda = (t) -> t.getStatus() < 10000;
Now, we have a class filterTrades that will filter trades based on which Lambda we have passed for filetering.
private List<Trade> filterTrades(ITrade trade, List<Trade> trades){
List<Trade> newTrades = new ArrayList<>();
for(Trade t : trades){
if(trade.check(t)){
newTrade.add(t);
}
}
}
So, in this way, it will behave differently based on Lambda passed to filterTrades() method.
Question 6:
What are the differences between Lambda and anonymous class?
Answer:
Below is the list of differences between Lambda and anonymous class:
- Usage of 'this' keyword: In anonymous class, this keyword refers to anonymous class itself. While in Lambda, this keyword refers to enclosing class of Lambda.
- Compilation: Anonymous class gets compiled to .class file in java. While Lambdas are compiled to private static methods in enclosing class. It uses invokedynamic bytecode instruction to bind this method dynamically.
- Lambdas implement a functional interface, so implements only 1 method. Anonymous class can extend a class or implement any number of interfaces.
Question 7:
What are the types of Lambdas?
Answer:
There are two types of Lambdas in Java 8:
Non-capturing Lambdas: These lambdas only use fields inside their bodies, as:
public class NonCapturingLambda{
public static void main(String[] args){
Runnable nonCapLambda = () -> System.put.println("NonCapturingLambda");
nonCapLambda.run();
}
}
Capturing Lambdas: These lambdas access fields outside their bodies, as:
public class CapturingLambda{
public static void main(String[] args){
String str = "CapturingLambda";
Runnable capLambda = () -> System.put.println("CapturingLambda = "+str);
capLambda.run();
}
}
That's all for this post. I'll share more questions in Part - II.
Thanks for reading.
No comments:
Post a Comment