- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 are Default Methods in Java 8?
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
In a separate class you need to implement this interface and provide body for all its abstract methods.
Example
interface MyInterface{ public void demo(); } public class InterfaceExample implements MyInterface{ public void demo() { System.out.println("This is the implementation of the demo method"); } public static void main(String args[]) { new InterfaceExample().demo(); } }
Output
This is the implementation of the demo method
If a new method is added to an interface, you need to provide body for this method in all the classes implementing its interface
Default method
From JSE8 Default methods are introduced in Java. Since then, you can write a default implementation to a particular method in an interface. If you do so, there is no need to implement these (default methods) in the classes that already implement this interface.
A default method is also known as defender method or virtual extension method. You can define a default method using the default keyword as −
default void display() { System.out.println("This is a default method"); }
Note − All the methods of an interface are public therefore there is no need to use public before it explicitly.
Example
Following Java Example demonstrates the usage of the default method in Java.
interface sampleInterface{ public void demo(); default void display() { System.out.println("This is a default method"); } } public class DefaultMethodExample implements sampleInterface{ public void demo() { System.out.println("This is the implementation of the demo method"); } public static void main(String args[]) { DefaultMethodExample obj = new DefaultMethodExample(); obj.demo(); obj.display(); } }
Output
This is the implementation of the demo method This is a default method
- Related Articles
- Java 8 default methods in interfaces
- Default methods in Java
- What is the use of default methods in Java?
- Can we override default methods in Java?
- What are Default Constructors in Java?
- What are methods in Java?
- What happens if we overload default methods of an interface in java?
- What are defender methods or virtual methods in Java?
- What are generic methods in Java?
- what are abstract methods in Java?
- What are vararg methods in Java?
- Java 8 static methods in interfaces
- What is default, defender or extension method of Java 8?
- What are the default array values in Java?
- How to solve diamond problem using default methods in Java
