• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface methods

 
Ranch Hand
Posts: 31
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.  



 
Ranch Hand
Posts: 62
5
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 31
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 31
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good job finding the JLS reference. Have a cow.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic