In my previous post, I have discussed some important Java SE 8 Interview Questions and Answers. In this post, we are going to discuss some more Java SE 8 Interview Questions and Answers. Before reading this post, please go through my previous post at: “Java SE 8 Interview Questions (Part 1)”.
Before Java 8, We don’t Internal Iteration concept. Java 8 has introduced a new feature known as “Internal Iteration”. Before Java 8, Java Language has only External Iteration to iterate elements of an Aggregated Object like Collections, Arrays etc. Internal Iteration means “Iterating an Aggregated Object elements one by one internally by Java API”. Instead of Java Application do iteration externally, We ask Java API to do this job internally.
S.No. | External Iteration | Internal Iteration |
---|---|---|
1. | Available before Java 8 too. | It is introduced in Java SE 8 |
2. | Iterating an Aggregated Object elements externally. | Iterating an Aggregated Object elements internally (background). |
3. | Iterate elements by using for-each loop and Iterators like Enumeration, Iterator, ListIterator. | Iterate elements by using Java API like “forEach” method. |
4. | Iterating elements in Sequential and In-Order only. | Not required to iterate elements in Sequential order. |
5. | It follows OOP approach that is Imperative Style. | It follows Functional Programming approach that is Declarative Style. |
6. | It does NOT separate responsibilities properly that is, it defines both “What is to be done” and “How it is to be done”. | It defines only “What is to be done”. No need to worry about “How it is to be done”. Java API takes care about “How to do”. |
7. | Less Readable Code. | More Readable code. |
External Iteration has the following drawbacks:
Compare to External Iteration, Internal Iteration has the following advantages:
Compare to External Iteration, Internal Iteration has one major drawback:
Compare to Internal Iteration, External Iteration has one major advantage:
We need to understand the situations to use either Internal Iteration or External Iteration.
S.No. | Stream Intermediate Operations | Stream Terminal Operations |
---|---|---|
1. | Stream Intermediate operations are not evaluated until we chain it with Stream Terminal Operation. | Stream Terminal Operations are evaluated on it’s own. No need other operations help. |
2. | The output of Intermediate Operations is another Stream. | The output of Intermediate Operations is Not a Stream. Something else other than a Stream. |
3. | Intermediate Operations are evaluated Lazily. | Terminal Operations are evaluated Eagerly. |
4. | We can chain any number of Stream Intermediate Operations. | We can NOT chain Stream Terminal Operations. |
5. | We can use any number of Stream Intermediate Operations per Statement. | We can use only one Stream Terminal Operation per Statement. |
In Java 7 or earlier, It is not possible to provide method implementations in Interfaces. Java 8 on-wards, it is possible. In Java SE 8, We can provide method implementations in Interfaces by using the following two new concepts:
A Default Method is a method which is implemented in an interface with “default” keyword. It’s new featured introduced in Java SE 8. We need Default Methods because of the following reasons:
A Static Method is an Utility method or Helper method, which is associated to a class (or interface). It is not associated to any object. We need Static Methods because of the following reasons:
Functional Programming | OOP |
---|---|
Does not exist State | Exists State |
Uses Immutable data | Uses Mutable data |
It follows Declarative Programming Model | It follows Imperative Programming Model |
Stateless Programming Model | Stateful Programming Model |
Main Fcous on: “What you are doing” | Main focus on “How you are doing” |
Good for Parallel (Concurrency) Programming | Poor for Parallel (Concurrency) Programming |
Good for BigData processing and analysis | NOT Good for BigData processing and analysis |
Supports pure Encapsulation | It breaks Encapsulation concept |
Functions with No-Side Effects | Methods with Side Effects |
Functions are first-class citizens | Objects are first-class citizens |
Primary Manipulation Unit is “Function” | Primary Manipulation Unit is Objects(Instances of Classes) |
Flow Controls: Function calls, Function Calls with Recursion | Flow Controls: Loops, Conditional Statements |
It uses “Recursion” concept to iterate Collection Data. | It uses “Loop” concept to iterate Collection Data. For example:-For-each loop in Java |
Order of execution is less importance. | Order of execution is must and very important. |
Supports both “Abstraction over Data” and “Abstraction over Behavior”. | Supports only “Abstraction over Data”. |
We use FP when we have few Things with more operations. | We use OOP when we have few Operations with more Things. For example: Things are classes and Operations are Methods in Java. |
NOTE:- For more information about FP, IP and OOP comparisons, Please go through my previous post at: “Compare FP, OOP(IP)”
Java’s OLD Java Date API means Date API available before Java SE 8 that is Date, Calendar, SimpleDateFormat etc. Java’s Old Date API has the following Issues or Drawbacks compare to Java 8’s Date and Time API and Joda Time API.
Java SE 8’s Date and Time API has the following Advantages compare to Java’s OLD Date API.
We need Java 8’s Date and Time API to develop Highly Performance, Thread-Safe and Highly Scalable Java Applications. Java 8’s Date and Time API solves all Java’s Old Date API issues by following Immutability and Thread-Safety principles.
Differences between Java’s OLD Java Date API and Java 8’s Date and Time API:
S.No. | Java’s OLD Java Date API | Java 8’s Date and Time API |
---|---|---|
1. | Available before Java 8 too. | It is introduced in Java SE 8 |
2. | Not Thread Safe. | Thread Safe. |
3. | Mutable API. | Immutable API. |
4. | Less Performance. | Better Performance. |
5. | Less Readability. | More Readability. |
6. | It’s not recommended to use as its deprecated. | It’s always recommended to use. |
7. | Not Extendable. | Easy to Extend. |
8. | It defines months values from 0 to 11, that is January = 0. | It defines months values from 1 to 12, that is January = 1. |
9. | It’s an old API. | It’s a new API. |
Multiple Inheritance means a class can inherit or extend characteristics and features from more than one parent class. In Java 7 or Earlier, Multiple Inheritance is not possible because Java follows “A class should extend one and only one class or abstract class” Rule. However, it’s possible to provide Multiple Implementation Inheritance using Interface because Java follows “A class can extend any number of Interfaces” Rule. However, Java 8 supports “Implementing Methods in Interfaces” by introducing new features: Default methods in Interface. Because of this feature, Java 8 supports Multiple Inheritance with some limitations.
Java 8 default methods can introduce a diamond problem when a class implements multiple interfaces. It occurs when a Class extends more than one interfaces with the same method implementations (Default method). Sample Java SE 8 Code to show the Diamond Problem with interface default methods.
interface A {
default void display() {
System.out.println("A");
}
}
interface B extends A {
default void display() {
System.out.println("B");
}
}
interface C extends A {
default void display() {
System.out.println("C");
}
}
class D implements B, C {
}
In the above code snippet, class D gives compile-time error as “Duplicate default methods named display with the parameters () and () are inherited from the types C and B”. It’s because Java Compiler will get confused about which display() to use in class D. Class D inherits display() method from both interfaces B and C. To solve this problem, Java 8 has given the following solution.
class D implements B, C {
@Override
public void display() {
B.super.display();
}
}
This B.super.display(); will solve this Diamond Problem. If you want to use C interface default method, then use C.super.display();
. That’s all about Java 8 Interview Questions. We will discuss some more Java SE 8 Interview Questions in my coming posts. Please drop me a comment if you like my post or have any issues/suggestions.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
The Diamond problem example given is wrong. It’s perfectly fine to implement multiple interfaces with default method common on parent class. 1. The closing brace in below statement got commented which is incorrect syntax: default void display() { //code goes here } 2. The program doesn’t show diamond problem it compiles correctly.
- Vaibhav Molawane
On multiple inheritance: Always ask what do you mean. Java does not support multiple inheritance of classes. However Java supports multiple inheritance of interfaces.
- DP
What will happen if we override display()method in B and C interface?
- Yogesh Pawar
In the above (diamond problem) example, I have not got any compile-time error. when default method of “Interface A” is overridden in “interface B” as well as “interface C” then we are getting compile-time error as follows: “Duplicate default methods named display with the parameters () and () are inherited from the types C and B”
- Preetesh Kumar
Good posts on Java SE 8 Interview questions and answers. Gives an idea on what type of questions we can expect from Interviewers to prepare. Great job.
- Srao
public interface A{ default void display() { //code goes here } } public interface B extends A{ } public interface C extends A{ } public class D implements B,C{ } You will not have compile error with your code above. You will only have compile error if classes B and C both have same display method signature.
- Andrew Minchekov
Class able to create default methods ? Please check and correct the code snippet.
- Muralikrishna
You dont have to have many duplicate questions. Otherwise, It is an informative blog.
- Palani
Interesting concepts and very understandable way of explanation. Thanks.
- Sriphani
Very Helpful Tutorial… Thanks!!!
- Anuj Gupta