- 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
Why java8 introduces default method in an interface?
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
It is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.
Example
interface MyInterface{ public void display(); public void setName(String name); public void setAge(int age); }
If you need your class to follow a certain specification you need to implement the required interface and provide body for all the abstract methods in that interface.
For example, if you want to create a thread, one way to do so is to implement the runnable interface and provide body to the abstract method run().
Example
public class RunnableDemo implements Runnable { private Thread t; private String threadName; RunnableDemo(String name) { threadName = name; } public void run() { System.out.println("implementation of the runnable method"); } public void start () { t = new Thread (this, threadName); t.start (); } public static void main(String args[]) { RunnableDemo r = new RunnableDemo("Thread_1"); r.start(); } }
Output
Creating Thread_1 implementation of the runnable method
If you do not provide the implementation of all the abstract methods of an interface (you, implement) a compile time error is generated.
Example
public class RunnableDemo implements Runnable { private Thread t; private String threadName; RunnableDemo( String name) { threadName = name; System.out.println("Creating " + threadName ); } /*public void run() { System.out.println("implementation of the runnable method"); }*/ public static void main(String args[]) { RunnableDemo r = new RunnableDemo( "Thread-1"); } }
Compile time error
RunnableDemo.java:1: error: RunnableDemo is not abstract and does not override abstract method run() in Runnable public class RunnableDemo implements Runnable { ^ 1 error
What if new methods are added in the interfaces?
Suppose we are using certain interface and implemented all the abstract methods in that interface and new methods were added later.
Then, all the classes using this interface will not work unless you implement the newly added methods in each of them.
To resolve this issue from Java8 default methods are introduced.
Default methods
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"); }
Once write a default implementation to a particular method in an interface. there is no need to implement it in the classes that already using (implementing) this interface.
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
Note − All the methods of an interface are public therefore there is no need to use public before it explicitly.
- Related Articles
- Default method vs static method in an interface in Java?
- Reference to an instance method using method references in Java8
- Difference between default and static interface method in Java 8.
- Interface variables are static and final by default in Java, Why?
- What are method references in Java8?
- Can we declare interface members as private or protected in java8?
- Why an interface cannot implement another interface in Java?
- Reference to a static method using method references in Java8
- What happens if we overload default methods of an interface in java?
- Reference to a constructor using method references in Java8
- Is it mandatory to override the default methods of an interface in Java?
- How to call an interface method in Java?
- Java.util.StringJoiner in Java8
- Why can't we define a static method in a Java interface?
- Why cannot we specify access modifiers inside an interface in C#?
