

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is default, defender or extension method of Java 8?
Java 8 introduces a new concept of default method implementation in interfaces. This capability is added for backward compatibility so that old interfaces can be used to leverage the lambda expression capability 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
public interface vehicle { default void print() { System.out.println("I am a vehicle!"); } }
Example
public class Java8Tester { public 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 { public void print() { Vehicle.super.print(); FourWheeler.super.print(); Vehicle.blowHorn(); System.out.println("I am a car!"); } }
Output
I am a vehicle! I am a four wheeler! Blowing horn!!! I am a car!
- Related Questions & Answers
- What are defender methods or virtual methods in Java?
- What are Default Methods in Java 8?
- Difference between default and static interface method in Java 8.
- Java 8 default methods in interfaces
- What is the difference between McAfee and Windows Defender?
- Java 8 Clock equals() Method
- Java 8 Clock millis() Method
- Java 8 Clock instant() method
- Java 8 Clock hashCode() method
- Java 8 Clock getZone() method
- Java 8 Clock fixed() method
- Java 8 Clock offset() method
- Collectors.joining() method in Java 8
- What is Functional Interface in Java 8?
- What is StringJoiner class in Java 8?
Advertisements