Home » Java 8 Default Methods

Java 8 Default Methods

by Online Tutorials Library

Java Default Methods

Java provides a facility to create default methods inside the interface. Methods which are defined inside the interface and tagged with default are known as default methods. These methods are non-abstract methods.

Java Default Method Example

In the following example, Sayable is a functional interface that contains a default and an abstract method. The concept of default method is used to define a method with default implementation. You can override default method also to provide more specific implementation for the method.

Let’s see a simple

Output:

Hello, this is default method Work is worship 

Static Methods inside Java 8 Interface

You can also define static methods inside the interface. Static methods are used to define utility methods. The following example explain, how to implement static method in interface?

Output:

Hello there Work is worship Helloooo... 

Abstract Class vs Java 8 Interface

After having default and static methods inside the interface, we think about the need of abstract class in Java. An interface and an abstract class is almost similar except that you can create constructor in the abstract class whereas you can’t do this in interface.

Output:

You can create constructor in abstract class Addition: 30 Substraction: 10 Multiplication: 200 
Next TopicJava 8 forEach

You may also like