Tutorial

Java 8 Interface Changes - static method, default method

Published on August 3, 2022
author

Pankaj

Java 8 Interface Changes - static method, default method

Java 8 interface changes include static methods and default methods in interfaces. Prior to Java 8, we could have only method declarations in the interfaces. But from Java 8, we can have default methods and static methods in the interfaces.

Java 8 Interface

java 8 interface changes, java 8 interface, java interface static method, java interface default method Designing interfaces have always been a tough job because if we want to add additional methods in the interfaces, it will require change in all the implementing classes. As interface grows old, the number of classes implementing it might grow to an extent that it’s not possible to extend interfaces. That’s why when designing an application, most of the frameworks provide a base implementation class and then we extend it and override methods that are applicable for our application. Let’s look into the default interface methods and static interface methods and the reasoning of their introduction in Java 8 interface changes.

Java Interface Default Method

For creating a default method in java interface, we need to use “default” keyword with the method signature. For example,

package com.journaldev.java8.defaultmethod;

public interface Interface1 {

	void method1(String str);
	
	default void log(String str){
		System.out.println("I1 logging::"+str);
	}
}

Notice that log(String str) is the default method in the Interface1. Now when a class will implement Interface1, it is not mandatory to provide implementation for default methods of interface. This feature will help us in extending interfaces with additional methods, all we need is to provide a default implementation. Let’s say we have another interface with following methods:

package com.journaldev.java8.defaultmethod;

public interface Interface2 {

	void method2();
	
	default void log(String str){
		System.out.println("I2 logging::"+str);
	}

}

We know that Java doesn’t allow us to extend multiple classes because it will result in the “Diamond Problem” where compiler can’t decide which superclass method to use. With the default methods, the diamond problem would arise for interfaces too. Because if a class is implementing both Interface1 and Interface2 and doesn’t implement the common default method, compiler can’t decide which one to chose. Extending multiple interfaces are an integral part of Java, you will find it in the core java classes as well as in most of the enterprise application and frameworks. So to make sure, this problem won’t occur in interfaces, it’s made mandatory to provide implementation for common default methods of interfaces. So if a class is implementing both the above interfaces, it will have to provide implementation for log() method otherwise compiler will throw compile time error. A simple class that is implementing both Interface1 and Interface2 will be:

package com.journaldev.java8.defaultmethod;

public class MyClass implements Interface1, Interface2 {

	@Override
	public void method2() {
	}

	@Override
	public void method1(String str) {
	}

	@Override
	public void log(String str){
		System.out.println("MyClass logging::"+str);
		Interface1.print("abc");
	}
}

Important points about java interface default methods:

  1. Java interface default methods will help us in extending interfaces without having the fear of breaking implementation classes.
  2. Java interface default methods has bridge down the differences between interfaces and abstract classes.
  3. Java 8 interface default methods will help us in avoiding utility classes, such as all the Collections class method can be provided in the interfaces itself.
  4. Java interface default methods will help us in removing base implementation classes, we can provide default implementation and the implementation classes can chose which one to override.
  5. One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.
  6. If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from java.lang.Object. The reasoning is very simple, it’s because Object is the base class for all the java classes. So even if we have Object class methods defined as default methods in interfaces, it will be useless because Object class method will always be used. That’s why to avoid confusion, we can’t have default methods that are overriding Object class methods.
  7. Java interface default methods are also referred to as Defender Methods or Virtual extension methods.

Java Interface Static Method

Java interface static method is similar to default method except that we can’t override them in the implementation classes. This feature helps us in avoiding undesired results incase of poor implementation in implementation classes. Let’s look into this with a simple example.

package com.journaldev.java8.staticmethod;

public interface MyData {

	default void print(String str) {
		if (!isNull(str))
			System.out.println("MyData Print::" + str);
	}

	static boolean isNull(String str) {
		System.out.println("Interface Null Check");

		return str == null ? true : "".equals(str) ? true : false;
	}
}

Now let’s see an implementation class that is having isNull() method with poor implementation.

package com.journaldev.java8.staticmethod;

public class MyDataImpl implements MyData {

	public boolean isNull(String str) {
		System.out.println("Impl Null Check");

		return str == null ? true : false;
	}
	
	public static void main(String args[]){
		MyDataImpl obj = new MyDataImpl();
		obj.print("");
		obj.isNull("abc");
	}
}

Note that isNull(String str) is a simple class method, it’s not overriding the interface method. For example, if we will add @Override annotation to the isNull() method, it will result in compiler error. Now when we will run the application, we get following output.

Interface Null Check
Impl Null Check

If we make the interface method from static to default, we will get following output.

Impl Null Check
MyData Print::
Impl Null Check

Java interface static method is visible to interface methods only, if we remove the isNull() method from the MyDataImpl class, we won’t be able to use it for the MyDataImpl object. However like other static methods, we can use interface static methods using class name. For example, a valid statement will be:

boolean result = MyData.isNull("abc");

Important points about java interface static method:

  1. Java interface static method is part of interface, we can’t use it for implementation class objects.
  2. Java interface static methods are good for providing utility methods, for example null check, collection sorting etc.
  3. Java interface static method helps us in providing security by not allowing implementation classes to override them.
  4. We can’t define interface static method for Object class methods, we will get compiler error as “This static method cannot hide the instance method from Object”. This is because it’s not allowed in java, since Object is the base class for all the classes and we can’t have one class level static method and another instance method with same signature.
  5. We can use java interface static methods to remove utility classes such as Collections and move all of it’s static methods to the corresponding interface, that would be easy to find and use.

Java Functional Interfaces

Before I conclude the post, I would like to provide a brief introduction to Functional interfaces. An interface with exactly one abstract method is known as Functional Interface. A new annotation @FunctionalInterface has been introduced to mark an interface as Functional Interface. @FunctionalInterface annotation is a facility to avoid accidental addition of abstract methods in the functional interfaces. It’s optional but good practice to use it. Functional interfaces are long awaited and much sought out feature of Java 8 because it enables us to use lambda expressions to instantiate them. A new package java.util.function with bunch of functional interfaces are added to provide target types for lambda expressions and method references. We will look into functional interfaces and lambda expressions in the future posts.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 22, 2014

great tutorials cleatly explains the concept without deviating from the topic. Awaiting for your future posts on functional interfaces and lambda expressions.

- Dilip Kumar Pandey

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
June 12, 2016

it’s already posted, search for it and you will find it. :)

- Pankaj

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 24, 2014

    Superb explanation , we are awaited for next post .

    - Ranjith

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      November 29, 2014

      We can use static interface methods to remove utility classes such as Collections and move all of it’s static methods to the corresponding interface, that would be easy to find and use.

      I like the new static interface methods feature, but I don’t think what you suggested here is a good idea - an interface is meant to be implemented, to expand the protocol of an object, whereas utility classes such as Collections are just containers for utility functions, which is a bit of a hack, as Java does not provide support for standalone functions. I think classes are a better choice. Given the newfound power of interfaces, we should leave them for what they are meant for, to define partial protocols (and implementations) for objects in an object-oriented program, and continue to use classes as a pattern for holding non-OO functionality. is that better than using classes?

      - Rafael Chaves

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      November 29, 2014

      Oh, I see now, you are talking about helper classes that were creared to complement an interface. In that case, I do see merit in your suggestion.

      - Rafael Chaves

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      June 12, 2016

      Thanks for the comment and understanding. :)

      - Pankaj

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        April 9, 2015

        Very nice explanation…Thanks…

        - Pramodkumar

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          May 4, 2015

          With interface default methods there may also be a scenario where an interface is extending another interface and both of those interfaces have same default method.

          - Anshudeep

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          June 12, 2016

          It will be the case of overriding super class method.

          - Pankaj

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            June 23, 2015

            Very good explanation indeed ! ! One very minor observation : In the class Interface1, line 9 -> is the print(str) a typo ? Thanks !

            - DRC

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            June 12, 2016

            yes it was typo, i have removed it and updated the post.

            - Pankaj

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              September 24, 2015

              Could you please explain how this got printed if we remove static from the interface method MyData Print::

              - Tejasvi

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              October 26, 2015

              When he removed the static qualifier, then the implementation class overrode the now non-static method isNull, so when he called print(“”), the interface called the implementation’s version of isNull, which returned ‘false’, allowing the print to occur.

              - Paul Ostrowski

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              June 12, 2016

              Thanks Paul for the explanation.

              - Pankaj

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 23, 2016

                Excellent explanation and useful replies. Thanks Pankaj for your blog entries! I suggest we replace line 8 of the MyDataImpl class to the shorter version shown below. It is more concise and common usage, while providing the same result as the current version. Thanks! return str == null;

                - JJ

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                June 12, 2016

                yes make sense, but to convey the idea easily I think it’s better to have ternary operator. It’s more like developers choice of using the code. Thanks!

                - Pankaj

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  June 7, 2016

                  hi, indeed i would also like to know what is with that print from line 9, Interface1, default method. Thanks in advance. regards

                  - Paula

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  June 12, 2016

                  Nice catch, i have removed that code and updated the post.

                  - Pankaj

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    June 9, 2016

                    Hi Pankaj - Thanks for such a concise and self-explanatory blogpost. Quick question - I am trying to understand why is it that Functional Interfaces can have ONLY one method ? Why not overloaded methods ? For example - operate(int a, int b) operate(float a, float b)

                    - IRS

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    June 9, 2016

                    I guess that is to keep things simple because method names are not used while calling them.

                    - Pankaj

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      October 25, 2016

                      The underlying answer to your question is the fact that Functions are not [yet] “First-class Citizens” in Java. This essentially means that while lambdas provide a nice, convenient syntax, there are still being wrapped within a interface under the hood [in other words, lambdas are backward compatible with these interfaces]. And because of this, Functional Interfaces can have only one method. Hope that helps.

                      - Shishir Biyyala

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        February 28, 2017

                        Functional interfaces can have only one ‘Abstract method’. Other methods like default or static may or may not exist.

                        - Alok

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          September 20, 2016

                          I didn’t got the point number 4 for static method points : We can’t define interface static method for Object class methods, we will get compiler error as “This static method cannot hide the instance method from Object”. public interface Interface { default void play(){ System.out.println(“this is play method”); } static boolean equals() { System.out.println(“Interface Null Check”); return true; } }

                          - udit sharma

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          December 27, 2016

                          static boolean equals() is not object equals method try below: static boolean equals(Object obj)

                          - anil

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            March 22, 2017

                            @Override public void log(String str){ System.out.println(“MyClass logging::”+str); Interface1.print(“abc”); } it should be Interface1.log(“abc”);

                            - sumit

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            March 4, 2018

                            It must be Interface1.super.log(“abc”) instead of Interface1.log(“abc”)

                            - NJ

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              March 22, 2017

                              @Override public void log(String str){ System.out.println(“MyClass logging::”+str); Interface1.print(“abc”); } it should be Interface1.super.log(“abc”);

                              - sumit

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                April 23, 2017

                                Hi Pankaj, thank you so much for the tutorials. And I couldn’t under stand the 6th point from default methods. “If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from java.lang.Object. The reasoning is very simple, it’s because Object is the base class for all the java classes. So even if we have Object class methods defined as default methods in interfaces, it will be useless because Object class method will always be used. That’s why to avoid confusion, we can’t have default methods that are overriding Object class methods.” So what exactly does this explain? does it say that “if any class in the hierarchy has a method with same signature as of the default method of base interface.” what does the compiler use: a) the implementing class method (or) b) the default method of the base interface?. (or) c)will the compiler throw an error. Thanks, Aruna.

                                - Aruna

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                February 2, 2018

                                It means if we have a method say void foo() in interface X and void foo() in Object class and a class Y implements X then using foo() method in Y will inherit it’s features from Object and not from X

                                - Niket

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                March 27, 2018

                                Its not that complicated. Take your example. Interface I has default method foo. Now Class X implements Interface I and provides implementation for foo, overriding essentially. Then Class Y extends Class X. So, we now have I --> X --> Y. If Y uses method foo, implementation of X is invoked; thereby rendering the default implementation in Interface I useless. Clear so far, with me? Now, second point. If Interface I provides default implementation of equals (a method in Object class); and now Class X implements Interface I, whose equals method will it use: Interface I’s or Object class’? Remember that Interface I and Object class have no connection. To avoid this confusion, the Java language architects added this check in the compiler to not allow default implementation of methods provided in the Object class already. Hope that clarifies.

                                - Tapan Singh

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  April 23, 2017

                                  Hi Pankaj, Is it mandatory for the implementation classes to use the default methods (or) is it optional to use default methods. I mean does the rule of abstract methods of interface apply to default methods making it mandatory for the implementation classes to have default methods in their code. Thankyou.

                                  - Aruna

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  June 1, 2017

                                  Its not mandatory as the interface is already providing a default definition and that’s the sole purpose.

                                  - Bhanu

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    April 23, 2017

                                    Hi Pankaj, Can you please explain the below conflict. Journaldev says “Designing interfaces have always been a tough job because if we want to add additional methods in the interfaces, it will require change in all the implementing classes. As interface grows old, the number of classes implementing it might grow to an extent that it’s not possible to extend interfaces. That’s why when designing an application, most of the frameworks provide a base implementation class and then we extend it and override methods that are applicable for our application.” But “By adding a default method to an interface, every implementing class automatically inherits this behavior. Some of these classes might have not been designed with that new functionality in mind, and this can cause problems. For instance, if someone adds a new default method default void foo() to an interface Ix, then the class Cx implementing Ix and having a private foo method with the same signature does not compile.” As per this again any changes to interfaces will effect the implementation classes right? thanks in advance, Aruna.

                                    - Aruna

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    January 29, 2018

                                    It will compile and class method will execute.

                                    - Ankit

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    April 28, 2018

                                    It won’t compile as it can result into reducing the visibility of the method, which can result into the compile time error.

                                    - Patan

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      May 7, 2017

                                      Hi Pankaj, Thanks again for useful content.

                                      - Niteesh Bhargava

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      September 2, 2017

                                      Dear nitish , it is good to see Dear pankaj thanks again

                                      - Sumit

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        June 30, 2017

                                        Can we create a static method in an interface and can i call class object with it’s static method.

                                        - Rajkumar

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        October 10, 2018

                                        Yes you can.

                                        - Khushboo

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          January 13, 2019

                                          If you want to call the static method defined in the interface then you would have to do it this way only InterfaceName.staticMethodName();

                                          - Varun

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          August 22, 2019

                                          can I not invoke the static method of the interface through the class it implements For example interface A { static void say() { system.out.println(“hello”); } public class B implements A { } public class C { public static void main(String args[]) { B.say(); } } will the above code not print “hello”. Should we invoke it through interface name?

                                          - Prathiba

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          November 14, 2020

                                          yes you can call static method of interface with the interface name

                                          - kunal

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            December 27, 2020

                                            this line will through compile time error - B.say(); it should be A.say()

                                            - DPS

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              November 22, 2017

                                              static is a keyword which makes any variable or method a class level variable or class level method ,means we can access it by our class name without making object of class. Thanks for sharing this.

                                              - priya

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                January 18, 2018

                                                Very well explained. Great job. Thanks very much for sharing.

                                                - Anil Gupta

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  January 21, 2018

                                                  Interface1.print(“abc”); there is no such method in Interface1. What is the main use of default method in interface? how can we access or make use of it?

                                                  - Paru

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  September 10, 2018

                                                  Main use of default method is basically to remove necessity of rather unavoidable base class that implements interface. Now generic common implementations don’t need to reside in base class, but it can be well included in interface as default method and all implementing class inherits it and directly use it, of course it can be overridden too.

                                                  - Zeck

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    March 9, 2018

                                                    awesome article, thanks man!

                                                    - sensibles

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      March 20, 2018

                                                      great explanation

                                                      - Varsha Beedkar

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        May 23, 2018

                                                        Strange !! but I can write main method in interface and execute it without writing main method in a class. I would like know about this behavior

                                                        - sharan

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        July 27, 2018

                                                        sahi pakde hain, in my view java is becoming weird in the name of new features.

                                                        - Vipin

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          April 18, 2019

                                                          i don’t think you can do that public class TestJava8 implements A { } interface A{ public static void main(String[] args){ System.out.print("Java "); } } when i run this code, i get Error: Main method not found in class com.codenamesid.applicationseries.algorithms.TestJava8, please define the main method as: public static void main(String[] args)

                                                          - Sudhakar

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          July 18, 2019

                                                          public interface DefaultInterface { public static void main(String[] args) { System.out.println(“Interface with Main”); } }

                                                          - karthik

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            July 7, 2020

                                                            Executing an interface - LOL

                                                            - Sriram

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              November 10, 2018

                                                              “If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from java.lang.Object. The reasoning is very simple, it’s because Object is the base class for all the java classes. So even if we have Object class methods defined as default methods in interfaces, it will be useless because Object class method will always be used. That’s why to avoid confusion, we can’t have default methods that are overriding Object class methods.” Can you please explain me this again? with some example. I am not able to comprehend the above point.

                                                              - Megha Krishnamurthy

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              December 16, 2018

                                                              The reasoning for “why a default method in an interface cannot override the methods of java.lang.Object” given in this blog is not clear to me as well. But as per what I read from other online materials, the reasoning is as follows: 1. The “java.lang.Object” class methods such as equals, hashCode and toString are all about an object state and it is the class that owns the object state. Interface does not own the object state. Hence, it is best if the class is determining what should go into the equals or hashCode or toString method rather than an Interface overriding these methods by means of ‘default’ methods. 2. Interfaces are designed for multiple inheritance. Consider the following class definition: class Foo implements Bar, Moo { // Implementation of Foo, that doesn’t override equals method } Suppose that the ‘Foo’ writer looks at the Bar and Moo interfaces, and finds that there is no implementation of ‘equals’ method and decides to use the default java.lang.Object class’ default equals method and implements his code accordingly. But later, someone has edited the Bar interface to include a default method that overrides the ‘equals’ method. Now, the equality functionality of Foo class is broken. To avoid these kind of issues, the Java designers decided to not to allow overriding any java.lang.Object class method.

                                                              - Sreedevi

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              December 17, 2018

                                                              As I have clearly mentioned, even if we define Object class methods in an interface, it will be useless because the Object class method will get called. That’s why I think it’ better to not even allow it.

                                                              - Pankaj

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              January 13, 2019

                                                              Hi Pankaj, I respect your work. It is very helpful. Yes, you did mention that Object class methods won’t be called. But Sreedevi has explained us as to why the developers of API have decided to go by that route. I found her explanation helpful. Thanks to both of you.

                                                              - Varun

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                January 24, 2019

                                                                Hi Pankaj, I went interview. in interview asked two quetions, 1) why one abstract method in functional Interface. 2)how to guess the types(data types) compile. please help me this question ansewer.

                                                                - rajesh

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                April 10, 2019

                                                                hi rajesh i would like to answer for your first question by contradiction we c’nt have more than one abstract method in functional interface because example : let suppose we allowed more than one abstract method in functional interface as you already know that in lamda expression we d;nt mention name of method , and your interface contain more than one abstract method than how compiler will know which method you wan to implement in your lamda expression is make any sense ? thank you

                                                                - tanuj1110

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                April 19, 2019

                                                                As tanuj1110 said its 50% agreed but I have one doubt. public Animal { void firstMethod(); int secondMethod(String str); } In the above example its good to recognize which method I am calling if I use lamda for example: Animal animal1 = s -> System.out.println(“Doing something”); Animal animal2 = (String s) -> return s.length(); In this scenario by seeing the method arguments we can get to know which method we are calling. Why java is not implemented in this way

                                                                - Ramachandrappa Kalavakuri

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                June 3, 2019

                                                                Ramachandrappa Kalavakuri: You have to accept what is given. Don’t suggest them(Oracle Developers) what to do and what not to what not to. And seems like you didn’t understand the lambda concept well.

                                                                - Akashh

                                                                  JournalDev
                                                                  DigitalOcean Employee
                                                                  DigitalOcean Employee badge
                                                                  June 30, 2019

                                                                  Hi Ramachandra your doubt Is correct but there is very huge issue in performance. Consider 100 methods in functional interface and for choosing the implementation Jvm needs to check 100 times type of return data and provide the implementation it is its one issue I guess there may be more.

                                                                  - Feroz

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    February 17, 2020

                                                                    you are able to call both method because those two method are declared in class ‘’'Animal". try interface Animal.

                                                                    - Ram Dafale

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      November 7, 2019

                                                                      Thank you for sharing. What’s next?

                                                                      - DAMARIS K. T. MAHAGNI

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        January 17, 2020

                                                                        Correct Explanation of static methods in java 8 interface. Thanks

                                                                        - sam

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          July 15, 2020

                                                                          If we able to define the default methods in interface now from Java 8, then what is the difference between abstract class and interface? And when to use what?

                                                                          - Bks

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          January 5, 2021

                                                                          abstract class is a class and interface is a interface, class has a state but interface don’t have any state and, In abstract class allows constructor and an interface don’t allow constructor.

                                                                          - Rakesh Burra

                                                                            JournalDev
                                                                            DigitalOcean Employee
                                                                            DigitalOcean Employee badge
                                                                            April 2, 2021

                                                                            Awesome blog on Strream API with examples. Thanks a lot Pankaj ji.

                                                                            - Sandy

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              June 27, 2021

                                                                              Thanks for the post. Have given good insights into default and static methods for interfaces. Just a suggestion, since we are talking about mitigating diamond problem in case of 2 interfaces having same default method, it might be a good idea, to show how a child class can invoke default method of one of the interfaces instead of calling static method to avoid diamond problem.

                                                                              - Manish Jain

                                                                                Try DigitalOcean for free

                                                                                Click below to sign up and get $200 of credit to try our products over 60 days!

                                                                                Sign up

                                                                                Join the Tech Talk
                                                                                Success! Thank you! Please check your email for further details.

                                                                                Please complete your information!

                                                                                Become a contributor for community

                                                                                Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                                                DigitalOcean Documentation

                                                                                Full documentation for every DigitalOcean product.

                                                                                Resources for startups and SMBs

                                                                                The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                                                Get our newsletter

                                                                                Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                                                New accounts only. By submitting your email you agree to our Privacy Policy

                                                                                The developer cloud

                                                                                Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                                                Get started for free

                                                                                Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                                                *This promotional offer applies to new accounts only.