This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Interfaces

 
Jehaan Butt
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code:
interface Bilbo {
// << insert code here
}
Which methods can be declared at the position indicated and not cause a compile-time error?

The options are:

a. public abstract void drawSting();
b. public static void drawSting();
c. void drawSting();
d. strictfp void drawSting();
e. protected void drawSting();

Answer: The correct options are A and C.
Interface method declarations are implicitly public and abstract, and these modifiers can be omitted. Interface methods cannot be static (ruling out C), nor can they be marked strictfp (ruling out A), final or native. Marking E protected conflicts with the restriction that interface methods must be public.

I have a doubt about the strictfp point. Why aren't interface methods meant to be marked strictfp? I am asking merely from a curious programmer's POV.

The question is from Java Inquisition's Tough SCJP questions test.
 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that a method declared in an interface must not be declared strictfp or native or synchronized, or a compile-time error occurs, because those keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared strictfp or native or synchronized in a class that implements the interface.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no use of marking an abstract method strictfp. If you override a method which is not strictfp, you can add the strictfp to the overriding method. Abstract methods don't have any body so won't have any floating point calculations in them, so marking them strictfp is completely useless...
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface are mainly used to provide abstract funtionallity and so that any user who implement the interface can have their own implementation.

User A may want his implementation use sctrictfp in his calculation where as user B does not need this.

So if you were able to put strictfp in the method declaration in the interface eventhough user B does not need sctrictfp behaviour,he is forced to do which is not the purpose of interface.

To keep it simpe no non access modifiers can be used with abstract modifiers in the method declaration.

Hope you are clear now.
 
Jehaan Butt
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, got it. Thanks all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic