• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is the 'Defining an Interface' section wrong? (Java OCA 8 Programmer I Study Guide, Sybex)

 
Patrick Nodder
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 267 the I think the 5th rule is wrong. Interface methods are only assumed to be public, they are not assumed to be abstract as well- am I right? Otherwise the answer to review question 1 on page 291 would be B and E. The explanation to review question 1 seems to back this up. Can someone clarify please?

Cheers



 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, interface methods are abstract by default. I believe you can use the 'abstract' keyword to make this property explicit, but if you don't, it doesn't make a difference.

An abstract method simply is one that doesn't have an implementation. Interface methods don't have an implementation (unless you use the 'default' keyword), so they're abstract.
 
Patrick Nodder
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I get it now, so it is only non-default and non-static methods that are assumed to be abstract. That's why the only correct answer is B; because the question is about all interface methods.
 
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
Patrick Nodder wrote:Can someone clarify please?

If you would have been preparing for the OCAJP7 certification exam, this topic would have been very easy. All methods defined in an interface are implicitly public and abstract. All constants defined in an interface are implicitly public, static, and final. So all the declarations in this interface are equivalent to each other

But you decided to take (and prepare) for the OCAJP8 certification exam. Due to some changes to the Java language, this topic is no longer very easy, but now it's just easy No changes were made to the constants, so that part is still the same and thus all constants defined in an interface are implicitly public, static, and final. In Java 8 an interface can contain abstract methods, default methods, and static methods. All these methods are implicitly public. Default methods are defined with the default modifier, and static methods with the static keyword. A method which is not defined with default or static is an abstract method, and only these methods are also implicitly abstract. And that's a well-thought decision of the Java designers, because this approach guarantees backwards compatibility So an interface defined in Java 7 (or earlier) will still compile successfully in Java 8 (and have the same behavior). In this code snippet all declarations of the same group (methods starting with the same prefix) are equivalentAlso note the difference in declarations of abstract methods versus default and static methods: abstract methods end with a semicolon, but no braces (an abstract method does not contain an implementation); default and static methods have an implementation and thus have curly braces.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic