Reasons behind why abstract and strictfp keywords cannot be used together in a method declaration?

StackOverflow https://stackoverflow.com/questions/16453119

  •  14-04-2022
  •  | 
  •  

Question

I'm reading SCJP by katherine sierra.
I understand that abstract and final keywords cannot be used together because they contradict each other as explained in the book.

However, I don't understand why strictfp and abstract keywords cannot be used together.
I don't know how the strictfp keyword exactly works in Java yet.

In my thoughts, one could declare an abstract strictfp method, have a subclass, and implement that method in the "strictfp way".

What is the reason these keywords don't get along well together ?
EDIT
I've double checked the book and it surely says

Because interface methods are abstract, they cannot be marked final, strictfp , or native .

from SCJP by Katherine Sierra. page 21.

Also my IDE(Eclipse Juno) says I can't use abstract and strictfp keywords together.
Hmmm, why not though ?

Was it helpful?

Solution

Katherine Sierra was probably talking about abstract methods. It would make no sense to make an abstract method strictfp because an abstract method just provides a method signature (and throws clause if any), to use it we need to override it in a subclass with a concrete method and this method will have its own modifiers which will override its parent method's modifiers. That is, methods do not inherit modifiers.

Note that it's not only sctriptfp, but no modifiers are allowed on abstract methods except public and protected. You'll get a compile-time error if you try.

OTHER TIPS

Hope you already got your answer by now if not then it may be useful for you.

I have encountered a similar question asked in a forum.

The reason why abstract and strictfp can't sit together in the method declaration is because abstract says the method must not be implemented by the current class and it must be implemented by the concrete subclass and strictfp says the method should be implemented (should have a body) by the class where strictfp is used. So in this case both key words contradict with each other hence both are not allowed together in method declarations.

But it is absolutely legal to use abstract and strictfp before class. Something like

public abstract strictfp class MyAbstractClass{}      //is ok

If you declare strictfp in a abstract class then all its method is going to be strictfp by default. Remember all the concrete methods in side the class not the abstract methods.

Run the example given below and see the OP:

import java.lang.reflect.*;
public abstract strictfp class AbstractStrictfp 
{
     public   abstract void abstractMethod();
     public   void concreteMethod(){};

     public static void main(String args[]){
          Method methods[] = AbstractStrictfp.class.getMethods();
          for(Method method : methods){
              System.out.println("Method Name::"+method.getName());
                     System.out.println("Modifiers::"+Modifier.toString(method.getModifiers()));
              System.out.println();

          }
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top