Modifiers in Java
There are two type of modifiers :
- Access modifiers
- Non-Access modifiers
1. Access modifiers
It is used to provide restriction to access the property.
There are 4 types of access modifiers:
- private
- default
- protected
- public
1. private
It is a predefine keyword.
It is highly restricted than others like default, protected and public.
It can be access with in a class only.
2. default
It is not a keyword.
It is less restricted than private.
It can be access with in a package only where package is group of classes.
In java by default property is default.
3. protected
It is a predefine keyword.
It is less restricted than default.
It can be access outside a package via inheritacne only.
4. public
It is a predefine keyword.
It is less restricted than protected.
It can be access from anywhere and in any manner.
2. Non-Access modifiers
It can never provide a restriction to access the property.
There are some following non-access modifiers :
- final
- abstract
- synchronized
- volatile
- transient
- strictfp
1. final
It is a keyword.
It is used to make any thing as a constant.
If top level class or inner class is final so it can never be inherited.
If any type of variable is final then you can never change the value because it works as a constant variable.
If your static method is final then child class can never use method hiding.
If your non static method is final then child can never overirde this method.
Final class object or instance can be instanciated.
2. abstract
It is a keyword.
It is used to declare unimplemented method or declare a method.
If your top level class or inner class is abstract then you can never create instance or object.
Abstract class can be inherited.
If abstract is used with non static method then you can never define its body.
Above these keywords are used with following things:
Note : We can discuss other keyword in details where they will be used.
No comments:
Post a Comment