The modifiers of a Java class

The modifiers of a Java class are:

  • public
  • abstract
  • final
  • strictfp

Th modifier public affects the visibility of the class, and now I need to define the visibility of a class.
For visibility of a class I mean the ability to:

  • instance the class: MyClass myClassImpl = new MyClass();
  • extend the class: class MyChildClass extends MyClass{…}

In these cases the class MyClass is visible, otherwise it is hidden.
A class not declared with the modifier public is visible only from the classes of its package and hidden to the classes belonging to other packages (default).
A class declared public is visible from the outside of its package and you must use an import [MyClass package]. MyClass.
Then there are 2 types of visibility (default and public) but only one access modifier (public).

Th modifier abstract means that at least one type of method is abstract, the class can not be initialized but only extended, and the extended class must be declared abstract if you don’t implement all methods.

The final modifier means that the class can not be extended, a typical example of a final class is the String class.

abstract and final are incompatible.

strictfp means the class follows the IEEE 754 standard about the floating-point math, then it ensures that the code is platform independent, although this often means less speed and accuracy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.