• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clarification in chapter 5 predicates in page 214 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya,
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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:But only one Abstract method. shouldn't this be like "functional interfaces can have only one abstract method but many default or static methods".

Yes! A functional interface is any interface that contains only one abstract method. A functional interface may contain one or more default methods or static methods.

And what do you think about this interface. Is it a functional interface or not?

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


equals method from the java.lang.Object which is the superclass of all classes.

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().
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it shouldnt compile. Well...maybe because hashcode() has a native specifier and Interfaces don't allow them.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh okk.....
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic