Biniman Idugboe wrote:
So, what does it mean when a static method is defined inside an Interface?
Biniman Idugboe wrote:System.out.println(doSomethingElse()); //not correct. Why? Does the class dissociates itself from a static method that belongs to an interface?
Biniman Idugboe wrote:What exactly does the word default add to the method definition? Does it mean that only static or default methods can ever be in an interface?
It is more a question of which of those will you write. Since those methods are override‑equivalent, a class or interface containing all those methods will fail to compile. You only write one of them; the custom is to omit the modifiers and write void myMethod();
Biniman Idugboe wrote:Thank you. But, bear with me as I have some more questions on this issue.
1. If Java regards all the above interface methods as being the same, which of the them does Java defaults to?
VB is a different language; forget all about it. Look in the Java® Language Specification (=JLS), where you will find that interface methods are implicitly public.
2. Some have public modifier and some do not have it. What is the difference? I remember that in VB, the default modifier is Public.
So there is no difference.
That JLS section wrote:Every method declaration in the body of an interface is implicitly public (§6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.
MyInterface.java:3: error: modifier private not allowed here
private void foo();
MyInterface.java:4: error: modifier protected not allowed here
protected void bar();
You will find a definition of functional interface in the same part of the JLS:-
3. I have also come across phrases such as Functional interface. At what point does an interface becomes a Functional interface. Is the example given above (myInterface) a functional interface?
It has one method and one method only which must be implemented in non‑abstract implementing classes (=abstract method). That doesn't include static methods, nor default methods, nor any of the nine public methods of java.lang.Object. You may find the details in the @FunctionalInterface annotation helpful, too. So Comparator is a functional interface; if you try to create a λ with it, the javac tool can work out that you need to implement the compare method and not one of the other methods. If you look at its methods and click the tab labelled abstract methods, only two methods will remain visible and the other twelve will be hidden. But you will see that one of the methods is equivalent to a method from Object, and it even says in the documentation that you do not need to implement it. It may be easier to visualise in the Java7 version, but both versions say the same thing. You will also see that the Java8 version is tagged with the @FunctionalInterface annotation.
A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. . . .
What does the JLS say? I think you will find that in this section. I think it says that all non‑private members of superinterfaces are inherited.After changing line 19 to read MyInterface.baz();
4. When you say that a default or a static method is not inherited in the class or the interface that implemented it, I suppose you mean, in my own words, the class or interface does not claim ownership of a default method or a static method that belongs to another class or interface. Correct? . . .
So you can see that the static method is not accessible in the implementing class, but the default method is.
Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.
The best way to make it possible to create a Stream from every List would be to add a stream() method to the List interface. Unfortunately, adding an abstract method to an interface would break all implementing code which didn't already have an implementation. Adding such a method to an abstract superclass wouldn't work because
Pete Letkeman wrote:. . . one of the big reasons for default methods was to provide some new life to existing packages/classes, specially collection classes, . . .