+ 3

Question regarding java

In java there is a default method which we use in interfaces . What is the use of default method and is it not effecting the main purpose of the interfaces?

14th Apr 2020, 10:10 AM
Ak Kumar
Ak Kumar - avatar
3 Respostas
+ 1
Hello Ak Kumar Public methods are abstract: You define only the method, but without a body: public void printMe(String s); When you try to implement a public method in an interface you will get compile error. A class which implements the interface must override this method. Since Java 8 you can implement default methods. A complete method with a body: default void printMe(String s){ System.out.println(s); } The class which implements the interface don't need to override default methods which makes sense because it is already implemented. method without any functionality -> override method with functionality -> no need to override (but of course you can) Why default methods? On reason is that you can add default methods to an existing interface without effecting other classes which implements this interface. I will try to write you later about more reasons and the consequences...
14th Apr 2020, 12:33 PM
Denise Roßberg
Denise Roßberg - avatar
0
What is the alternatuve of that?
14th Apr 2020, 11:38 AM
Ak Kumar
Ak Kumar - avatar