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 ?
strictfp can be applied to class, method or on interfaces but cannot be applied to abstract methods, variable or on constructors.
For classes, final is used to prevent inheritance whereas abstract classes depends upon their child classes for complete implementation. In cases of methods, final is used to prevent overriding whereas abstract methods needs to be overridden in sub-classes.
strictfp is an obsolete and unused reserved word in the Java programming language. Previously, this keyword was used as a modifier that restricted floating-point calculations to ensure portability.
The abstract keyword is a non-access modifier, used for classes and methods: Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body.
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.
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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With