
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the use of default methods in Java?
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.
- 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.
- If you do not provide the implementation of all the abstract methods of an interface (you, implement) a compile time error is generated.
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.
Following Java Example demonstrates the usage of the default method in Java.
Example
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
- Default methods in Java
- What are Default Methods in Java 8?
- Java 8 default methods in interfaces
- Is it mandatory to override the default methods of an interface in Java?
- What happens if we overload default methods of an interface in java?
- Can we override default methods in Java?
- What is the use of in flush() and close() methods of BufferedWriter class in Java?
- What is the use of tail() methods in Pandas series?
- What is the use of head () methods in Pandas series?
- What is the use of abs() methods in pandas series?
- How to solve the diamond problem using default methods in Java?
- What is the purpose of a default constructor in Java?
- What is the scope of default access modifier in Java?
- How to solve diamond problem using default methods in Java
- What is Default access level in Java?
