Java Interface in JAVA version 8
Dated: 20-3-2014
In Java 8,
1. Interfaces allow static method Implementation.
2. Interfaces also allow default Instance method Implementation called as default methods.
3. Interfaces support Multiple Inheritance for the implemented methods present in Interface.
4. SAM- "Single Abstract Method" Interface (Interface with single abstract method only) are called "Functional Interfaces"
5. Functional Interface Types (Object/Class/Interface type) are called as "Target Types" as they will be inferred during the Lambda expression evaluation based on the context.
6. Anonymous Implementations of Functional Interfaces can be replaced by "Lambda Expressions", if no state/field required to be present. Lambda Expressions actually allows us to create Anonymous methods instead of Anonymous class Implementations, just as like our functional languages, but still it is Object Oriented and follows "Bottom up" programming paradigm.
It was surprising to see that interfaces more or less turning closer like an abstract classes by allowing us to provide Implementations, except for not allowing states/fields. It was also surprising to see that JAVA which was allowing multiple inheritance only for abstract method by having a "ignore" rule, now also supports Multiple Inheritance of method Implementations. But it is really not so surprising because, the earlier Multiple Inheritance is "Multiple Inheritance of Type" only allowed for Interface type, which did not had any state/field or method Implementation, now in Java 8, "Multiple Inheritance of Implementation" is allowed as conflicts can be easily resolved by overriding/hiding the method implementations as the method implementations at interface level can only remain independent of any states/fields. "Multiple Inheritance of State" is not possible and not of any interest as they are not allowed in interface as they will introduce illicit consequences.
The main objective of having Implementations in Interface is to achieve Interface evolution. Now one can add any default method or static method without worrying that implementations will get affected.
Though I am not a polyglot programmer, I did encounter few concepts like "Traits" & "Structural Typing" while I was investigating Lambda features. which led to a thought that JAVA architects have Introduced "Traits" & "Structural Typing" in JAVA and as they allow us to have implementation in the upper most hierarchy and as they introduced Functional Interface & Target Typing concepts.
Though they are not supported in Java considering the Language Design evolution, I am reviewing them as it is good to know that such concepts do exists and to understand how closer Java Interfaces are present with respect to these concepts.
Traits are actually behaviors which are common to many non related classes. They are not specific to a particular class. Java Programmers should have come across many utility classes containing behavior to be applied to many required classes such as Mixins classes. The utility classes behaviors are good choice to be made as traits, but one cannot consider all behaviors in utility classes as traits. Traits are supported with specific language constructs in language such as Scala, PHP and in C++ with Templates.
To Introduce Structural Typing, let me take a different path. Usually functions will take primitive types, Object types as parameters, we naturally encounter them in JAVA. In languages like JavaScript, we can pass function handle as a parameter i.e., one function will take, another function as parameter based on their name. On the similar line, "Structural Typing" allows one to provide method/functions as parameters based on their signature. Any method/function which matches the signature can be passed as parameter.
My initial thoughts were like "Target Typing" is equivalent to "Structural Typing" and method implementation in Interfaces is equivalent to "Trait", but after reading "State of Lambda" article, I clearly understood that the intent was not to introduce them. Structural typing is not taken into consideration for Lamda expression attributing to language evolution constraints. And method implementation in interfaces though could act as Traits, the main intent was not implementing Traits to its fullest extend.
Though "Traits" were not intentionally introduced, method implementations in interfaces can be used in various non related Interface Implementations/Classes similar to Traits. And Mixing of method implementations similar to Mixins can also be achieved with the new Interfaces. Structural Typing though not supported, Lambda Expressions stands very close to them as they depend only on SAM/Functional Interfaces.
If Interfaces prior to JAVA 8 are better in design perspective, Interfaces post JAVA 8 are much better as they increase granularity by taking the behaviors closer to interfaces, increases re-usability by allows us to change Interfaces with ease without worrying much about the Implementations of Interface and allows us to write anonymous methods in the form of Lambda expression being a Functional Interface & Target Types.
Comments
Post a Comment