blog

Home / DeveloperSection / Blogs / Class modifier

Class modifier

Anonymous User809 06-Sep-2018

CLASS MODIFIERS

Whenever we are writing our own java class we have to provide some information about our class to the jvm like:

• The class can be accessed from anywhere or not?

• The child class creation is possible for our class or not?

• Whether instantiation is possible or not

We can specify this information by using appropriate modifier

For top-level class modifiers applicable are:

1. Public

2. <default>

3. Final

4. Abstract

5. Strictfp

If we are using any other modifier then we get compile time error

But for inner classes we can use:

1. Public

2. <default>

3. Final

4. Static

5. Abstract

6. Strictfp

7. Private

8. Protected

• Public classes:

These are the classes which can be accessible from anywhere.

• default classes:

These are the classes which can be accessible to same package only i.e. from outside we cannot access.

• final modifier:

a final modifier can be used for class, variable and methods.

The final class cannot be subclassed

Once a final variable is assigned then it holds the same value If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object

Final method cannot be overridden in child classes.

Every method in final class is final by default nut every variable is not.

The main purpose of final modifier is to achieve security.

• abstract modifier:

it is applicable for methods and classes only not for variables

1. abstract method:

it can have only declaration but not implementation and hence it should end with ; .

Child classes are responsible for the implementation. If a class include abstract method then class should be declared as abstract.

e.g public abstract void m1();

2. abstract class:

for any java class if we don’t want instantiation then we have to declare that class as abstract it cannot be subclassed.it may or may not include abstract method.

• Strictfp:

                It is a keyword used to restrict the floating-point system in java programming language.

For inner classes modifiers are:

• Static modifier:

Static modifier is applicable for only variables and methods.

Static variable: a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program.

Static method: A static method belongs to the class rather than object of a class. A static method does not creation of instance of a class. static method can access static member and can change the value of it.

• Private modifier:

If a member is declared as private then it can be accessed within the current class only.

• Protected modifier:

If a member is protected then we can access that member within the current package anywhere but outside package only in a child class.

Within the current package we can access protected members either by child reference or by parent reference or by child reference.



Updated 06-Sep-2018
I am a content writter !

Leave Comment

Comments

Liked By