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

I am reading SCJP from katherine sierra.
I understand that abstract and finite keywords cannot be used together because they contradict each other, as described in the book.

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

In my mind, you could declare an abstract strictfp method, have a subclass, and implement this method in the "strictfp way".

What is the reason that these keywords do not fit together?
EDIT
I double-checked the book and it undoubtedly says

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

from SCJP Katherine Sierra. page 21.

Also my IDE (Eclipse Juno) says that I cannot use abstract and strictfp keywords.
Hmmm, why not?

+6
source share
2 answers

Katherine Sierra probably spoke of abstract methods. There is no point in making the strictfp abstract method, because the abstract method just provides the method signature (and the throws if any clause), to use it, we need to redefine it in the subclass with a specific method, and this method will have its own modifiers that override the modifiers of the parent method . That is, methods do not inherit modifiers.

Note that this is not only sctriptfp , but modifiers are not allowed for abstract methods other than public and protected . If you try, you will get a compile time error.

+6
source

I hope you already received your answer, if not, then it may be useful to you.

I came across a similar question asked on the forum.

The reason abstract and strictfp cannot sit together in a method declaration is because abstract says that this method should not be implemented by the current class and should be implemented by a specific subclass and strictfp says that the method should be implemented (should have body) by a class where strictfp used. Therefore, in this case, both keywords contradict each other, therefore both of them are not allowed together in method declarations.

But it is absolutely legal to use abstract and strictfp in front of the class. Sort of

 public abstract strictfp class MyAbstractClass{} //is ok 

If you declare strictfp in an abstract class, then by default its entire method will be strictfp. Remember all the specific methods in the class, not abstract methods.

Run the example 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(); } } } 
+2
source

Source: https://habr.com/ru/post/944586/


All Articles