Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 551
Ascendant Avatar asked May 09 '13 01:05

Ascendant


People also ask

Is it possible to declare a variable with strictfp?

strictfp can be applied to class, method or on interfaces but cannot be applied to abstract methods, variable or on constructors.

Where do we use final and abstract keyword?

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.

What is strictfp keyword?

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.

What is the use of abstract keyword in regard to class and methods in Java explain with the help of an example?

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.


2 Answers

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.

like image 153
Evgeniy Dorofeev Avatar answered Nov 12 '22 06:11

Evgeniy Dorofeev


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();

          }
     }
}
like image 28
Bibhuti Pradhan Avatar answered Nov 12 '22 08:11

Bibhuti Pradhan