What is the purpose of the default keyword in Java?

An interface in Java is similar to a class, but an interface body can include only abstract methods and final (Constants).

I recently saw a question similar to this

 interface AnInterface { public default void myMethod() { System.out.println("D"); } } 

According to the interface definition, only abstract methods are allowed. Why does this allow me to compile the above code? What is the default keyword?

On the other hand, when I tried to write below code, then it says modifier default not allowed here

 default class MyClass{ } 

instead

 class MyClass { } 

Can someone tell me the purpose of the default keyword? Is this allowed only inside the interface? How does it differ from default (without an access modifier)?

+72
java java-8 interface default
Jul 23 '15 at 4:55
source share
8 answers

This is a new Java 8 feature that allows interface provide an implementation. Described in Java 8 JLS-13.5.6. An declaration of an interface method that reads (in part)

Adding a default method or changing a method from abstract to default does not violate compatibility with pre-existing binaries, but can cause IncompatibleClassChangeError if pre-existing binary code tries to call the method. This error occurs if the qualification type T is a subtype of two interfaces, I and J , where both I and J declare a default method with the same signature and result, and neither I nor J is an additional element of the other.

What's New in JDK 8 (Partially)

The default methods allow you to add new functionality to library interfaces and provide binary compatibility with code written for older versions of these interfaces.

+55
Jul 23 '15 at 4:59
source share

Default methods were added in Java 8 mainly to support lambda expressions. Designers (in my opinion, cleverly) decided to create lambda expression syntax to create anonymous interface implementations. But these lambdas can implement only one method, they will be limited to interfaces with one method, which will be a pretty serious limitation. Instead, default methods have been added to allow more sophisticated interfaces.

If you need some convincing assertion that default was introduced because of lambdas, note that the 2009 proposal by Mark Reinhold, the Project Lambda straw offer , mentions “extension methods” as a required feature added to support lambdas.

Here is an example demonstrating the concept:

 interface Operator { int operate(int n); default int inverse(int n) { return -operate(n); } } public int applyInverse(int n, Operator operator) { return operator.inverse(n); } applyInverse(3, n -> n * n + 7); 

Very far-fetched, I understand, but I have to show how, by default supports lambdas. Since inverse is the default, it can easily be overridden by the implementing class if required.

+21
Jul 23 '15 at 5:55
source share

Java 8 introduces new concepts called default methods. Default methods are those methods that have some default implementation and help in developing interfaces without breaking existing code. Let's look at an example:

  public interface SimpleInterface { public void doSomeWork(); //A default method in the interface created using "default" keyword default public void doSomeOtherWork(){ System.out.println("DoSomeOtherWork implementation in the interface"); } } class SimpleInterfaceImpl implements SimpleInterface{ @Override public void doSomeWork() { System.out.println("Do Some Work implementation in the class"); } /* * Not required to override to provide an implementation * for doSomeOtherWork. */ public static void main(String[] args) { SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl(); simpObj.doSomeWork(); simpObj.doSomeOtherWork(); } } 

and output:

Doing some work in class
DoSomeOtherWork implementation in interface

+12
Jul 23 '15 at 5:30
source share

Something that was missing in the other answers was his role in the annotations. Back in Java 1.5, the default keyword appeared as a means to provide a default value for an annotation field.

 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Processor { String value() default "AMD"; } 

This use has been overloaded by the implementation of Java 8 so that you can define a default method in the interfaces.

Something else that was missing: the reason the default class MyClass {} not valid is due to the way classes are declared in general . There is no provision in this language to display this keyword. However, it appears for interface method declarations .

+8
Feb 11 '18 at 7:25
source share

The new Java 8 feature ( default methods ) allows the interface to provide an implementation when it is marked with the default keyword.

For example:

 interface Test { default double getAvg(int avg) { return avg; } } class Tester implements Test{ //compiles just fine } 

An interface test uses the default keyword, which allows an interface to provide a standard method implementation without the need to implement these methods in classes that use the interface.

Backward compatibility. Imagine that your interface is implemented by hundreds of classes, and this interface will force all users to implement the newly added method, although it is not essential for many other classes that implement your interface.

Facts and limitations:

1-May is only declared inside an interface, not inside a class or abstract class.

2 - must provide the body

3 - This is not supposed to be public or abstract, like other common methods used in an interface.

+2
Aug 05 '16 at 16:57
source share

For a very good explanation, see Java ™ Tutorials , part of the explanation is:

Consider an example in which computer-controlled computer manufacturers participate who publish industry interfaces that describe what methods can be used to control their cars. What if these computer-controlled automakers add new features, such as flying, to their cars? These manufacturers will need to point out new methods that allow other companies (such as manufacturers of electronic overhead instruments) to adapt their software to aircraft. Where will these automakers announce these new flight-related practices? If they add them to their original interfaces, then the programmers who implemented these interfaces will have to rewrite their implementations. If they add them as static methods, then programmers will consider them as useful methods, and not as basic, basic methods.

The default methods allow you to add new functionality to the interfaces of your libraries and provide binary compatibility with code written for older versions of these interfaces.

+2
Nov 14 '16 at 15:15
source share

The default methods allow you to add new functionality to the interfaces of your applications. It can also be used for multiple inheritance . In addition to the default methods, you can define static methods in interfaces. This makes it easier for you to organize helper methods.

+1
Jul 23 '15 at 5:06
source share

The default methods in the interface allow us to add new functionality without breaking the old code.

Prior to Java 8, if a new method was added to an interface, all implementation classes of this interface were required to override this new method, even if they did not use the new functionality.

In Java 8, we can add a default implementation for a new method using the default keyword before implementing the method.

Even with anonymous classes or functional interfaces, if we see that some code can be reused, and we don’t want to define the same logic everywhere in the code, we can write their default implementations and reuse them.

example

 public interface YourInterface { public void doSomeWork(); //A default method in the interface created using "default" keyword default public void doSomeOtherWork(){ System.out.println("DoSomeOtherWork implementation in the interface"); } } class SimpleInterfaceImpl implements YourInterface{ /* * Not required to override to provide an implementation * for doSomeOtherWork. */ @Override public void doSomeWork() { System.out.println("Do Some Work implementation in the class"); } /* * Main method */ public static void main(String[] args) { SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl(); simpObj.doSomeWork(); simpObj.doSomeOtherWork(); } } 
0
Jun 19 '19 at 5:48
source share



All Articles