It says that "Lambdas work with functional interfaces that have only one method.These are called functional interfaces"..But in the functional interface in code below, I am able to create many default methods and static methods . But only one Abstract method. shouldn't this be like "functional interfaces can have only one abstract method but many default or static methods".
It is true that default and static methods don't count in determining whether an interface is a functional interface. Neither of those method types are on the OCA exam though so we don't mention it. In our OCP book, we do make this distinction.
Actually it does let me create boolean equals(Object obj); and also lets me have lambda expressions in my class, so I guess its still a functional interface....trying to decipher whats in lambdafaq
I guess they let you have abstract methods which are inherited abstract methods override equivalent of the super interfaces of the functional interface .. what is override equivalent and not being identical but related by erasure ..
and it doesn't let me create any other abstract method which are not like above.
Ramya Subraamanian wrote:Actually it does let me create boolean equals(Object obj); and also lets me have lambda expressions in my class, so I guess its still a functional interface
Yes! It definitely is a functional interface, although there seem to be two abstract methods. But one of those methods should be very familiar to you... Which one? And where do you know that method from?
Ramya Subraamanian wrote:I guess java.lang.Object is the superclass which lets the functional Interface override its equals method. Now I am wondering why it doesnt let me override public String toString().
Almost correct! An interface can never inherit from a class, therefore it's impossible to override a method of a class in an interface. But when counting the number of abstract methods, you can ignore the public methods defined in the Object class. So it should be no problem to add the (public) toString() method as well. Although this interface has 3 abstract methods, it's still a functional interfaceBut if the method signature differs from the method signature in the Object class, the code won't compile as it is not a functional interface anymore. This code does not compileAnd note that only public methods from the Object class are allowed. If you try adding the clone() method (which is protected) you'll get a compiler error as wellAnd adding a public final method from the Object class results in a compiler error too
Let's see if you have understood all the above. Will this code snippet compile? If not, why?
Ramya Subraamanian wrote:it shouldnt compile. Well...maybe because hashcode() has a native specifier and Interfaces don't allow them.
It doesn't compile. But the reason why is much easier! The Moveable interface has 4 abstract methods and only 2 of them have the method signature of a public method of the Object class. Because if you look closely the method name is hashcode (with lower case c) and that's different from the method hashCode in the Object class (which has an upper case C). A very tiny but very important difference! Therefore this interface has 2 abstract methods which is not allowed and therefore it doesn't compile.
I like to note that you can expect such kind of questions on the actual exam. That's one of the reasons why you need to pay close attention to the smallest details (even during studying and preparation). So on the actual exam you have to be fully focused during the complete exam. You can't afford to be less concentrated for a few minutes, because that might result in a few wrongly answered questions. And in the end that definitely could be the difference between passing or failing the exam.