Author: Editorial Team
-
Difference between interface with default method and abstract class or Java 8 interface vs abstract class
After introducing default method in Java 8, interfaces and abstract class seems to look same. However, they are actually different even in Java 8. The purpose of these two is still different. The main purpose of introducing default method is to introduce new features but maintaining backward compatibility. If you wish to add a method…
-
default methods in interface in case of multiple inheritance
When a class implements two interfaces, each having default methods with same signature, it results in compile time error. To understand this with an example, we have created two interfaces: Left and Right. Class Test implements these two interface. On compilation, above code will result in following error: The reason for above error is that…
-
default method in interface – Java 8
1. Introduction Before Java 8, interfaces could have only abstract methods. Java 8 has introduced default method which allows interfaces to have methods with implementation. default methods are declared with default keyword at the beginning of the method signature. 2. Need for default methods in Java interface Consider a requirement when you want to add…
-
Local variables referenced from a lambda expression must be final or effectively final
As per Java 8 Language Specification: Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared final or be effectively final (§4.12.4), or a compile-time error occurs where the use is attempted. A variable which is not declared as final but whose value is never…
-
Difference between anonymous inner class and lambda expression
Following are the differences between anonymous inner class and lambda expression: In the following code, ‘this’ refers to the current inner class object. So, when we print this.score, the output is 100. In the following code, ‘this’ refers to enclosing class object . So, when we print this.score, the output is 0.
-
Java 9 – Explain differences between JAR file and module
Differences between JAR file and module can be summarized in following points: JAR Module JAR stands for Java Archive and is a file format based on the ZIP file format. It is used for aggregating many files and associated metadata and resources(text, images etc) into one file to distribute as a library or application software.…
-
Need of JPMS – Problems before Java 9
We’ll see in this tutorial, why JPMS was needed and what problems did JPMS solved. Lack of strong encapsulation Encapsulation can be achieved by using a combination of packages and access modifiers (such as private, protected, or public). When you make a class public, then it is public for everyone. There is no way to…
-
JPMS modularity and module – an introduction
1. Introduction Java 9 introduced a new module system that makes modularization of applications easier. Java 9 modularity is built on top of the abstractions Java already has for encapsulations. Introduction of modules in Java 9 was a very big change as it resulted changes in JVM, the standard libraries and in the Java language…
-
Comparing Arrays for equality in Java
Two arrays are said to be equal if the arrays have equal number of elements and all corresponding pairs of elements in two arrays are equal. Method 1: Iterative method Following is the logic: This method is not not recommended for large sized array. Method 2: Using Arrays.equals() Arrays.equals() method returns true if the two…
-
Java program to find duplicate characters in a String
Duplicate characters with count from string using loop Following is the logic: Output Duplicate characters with count from string using map Following is the logic: Output
