• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface methods  RSS feed

 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

I've encountered the following, seemingly easy,  question: " What modifiers are implicitly applied to all interface methods?" (Java 8). The answer came really easy to me: public and abstract. Just to see that abstract was not correct anymore, because with the introduction of default and static methods the book says that it is not implicitly applied anymore. So I've opened my IntelliJ IDEA and after a short test, I've noticed that even in Java 8, when in an interface, you write "void myMethod();" ,it seems that the two modifiers public and abstract are implicitly applied. And even more is that when I  try to apply the abstract modifier "abstract void myMethod();", IntelliJ says that it is redundant.

The book is OCA by J. Boyarsky and S. Selikoff. Question 1 from the Review Questions / Chapter 5. 



 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Java Language Specification 8 in §9.4:

"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."


"An interface method lacking a default modifier or a static modifier is implicitly abstract, so its body is represented by a semicolon, not a block. It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration."
 
Richard Hayward
Ranch Hand
Posts: 186
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alexandru,

I've been using little test programs like this to clarify such issues:

output:

showing interface methods to be public abstract whether explicitly so declared or not.

However, I'm using a java 7 compiler. I'm guessing, that for compatibility and from the jls extract Andrea has given, I'd get identical output were I to compile with java 8?
 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, the answer is the book is incorrect. It this case I think if I will ever encounter this question in the exam, I will go with "the both are implicitly applied" answer.
 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice little test program! I've wouldn't have though at making one.

I've done the test in Java 8 and the output is the same:

m1 public abstract
m2 public abstract
m3 public abstract
m4 public abstract

Thank a lot for your answers guys! I know it is not necessarily something that you would hit yourself with in a real live working environment, but questions like this can be on the exam.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job finding the JLS reference. Have a cow.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!