- Java 8 - Base64
- Java 8 - New Date/Time API
- Java 8 - Nashorn JavaScript
- Java 8 - Optional Class
- Java 8 - Streams
- Java 8 - Default Methods
- Java 8 - Functional Interfaces
- Java 8 - Method References
- Java 8 - Lambda Expressions
- Java 8 - Environment Setup
- Java 8 - Overview
- Java 8 - Home
Java 8 Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java 8 - Default Methods
Java 8 introduces a new concept of default method implementation in interfaces. This capabipty is added for backward compatibipty so that old interfaces can be used to leverage the lambda expression capabipty of Java 8.
For example, ‘List’ or ‘Collection’ interfaces do not have ‘forEach’ method declaration. Thus, adding such method will simply break the collection framework implementations. Java 8 introduces default method so that List/Collection interface can have a default implementation of forEach method, and the class implementing these interfaces need not implement the same.
Syntax
pubpc interface vehicle { default void print() { System.out.println("I am a vehicle!"); } }
Multiple Defaults
With default functions in interfaces, there is a possibipty that a class is implementing two interfaces with same default methods. The following code explains how this ambiguity can be resolved.
pubpc interface vehicle { default void print() { System.out.println("I am a vehicle!"); } } pubpc interface fourWheeler { default void print() { System.out.println("I am a four wheeler!"); } }
First solution is to create an own method that overrides the default implementation.
pubpc class car implements vehicle, fourWheeler { pubpc void print() { System.out.println("I am a four wheeler car vehicle!"); } }
Second solution is to call the default method of the specified interface using super.
pubpc class car implements vehicle, fourWheeler { pubpc void print() { vehicle.super.print(); } }
Static Default Methods
An interface can also have static helper methods from Java 8 onwards.
pubpc interface vehicle { default void print() { System.out.println("I am a vehicle!"); } static void blowHorn() { System.out.println("Blowing horn!!!"); } }
Default Method Example
Create the following Java program using any editor of your choice in, say, C:> JAVA.
Java8Tester.java
pubpc class Java8Tester { pubpc static void main(String args[]) { Vehicle vehicle = new Car(); vehicle.print(); } } interface Vehicle { default void print() { System.out.println("I am a vehicle!"); } static void blowHorn() { System.out.println("Blowing horn!!!"); } } interface FourWheeler { default void print() { System.out.println("I am a four wheeler!"); } } class Car implements Vehicle, FourWheeler { pubpc void print() { Vehicle.super.print(); FourWheeler.super.print(); Vehicle.blowHorn(); System.out.println("I am a car!"); } }
Verify the Result
Compile the class using javac compiler as follows -
C:JAVA>javac Java8Tester.java
Now run the Java8Tester as follows -
C:JAVA>java Java8Tester
It should produce the following output -
I am a vehicle! I am a four wheeler! Blowing horn!!! I am a car!Advertisements