- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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?
- Can we declare an abstract method, private, protected, public or default in java?
- What is the use of default methods in Java?
- What is the purpose of toString() method? Or What does the toString() method do in java?
- Any Extension method in C#
- What is Default access level in Java?
- Default method vs static method in an interface in Java?
- What is the purpose of a default constructor in Java?
- What is the scope of default access modifier in Java?
- What are the default values of instance variables whether primitive or reference type in Java?
- Collectors.joining() method in Java 8

Advertisements