Tuesday, April 1, 2014

JAVA CONCEPTS, Modifiers - Part1

While writing First Java Program you must have encountered the word "public" and while using it you must be thinking what is use of this keyword? is it required? If you will not use it will it make much difference ?

In java we call "public" as the access-modifiers. In java there are two types of modifiers:
  • Access modifiers : public , protected, private and default (package-level)
  • Non-access modifiers : final abstract and strictfp
As we can write many variables, methods and different blocks like constructor, static block,instance block in java, same way we can use different access modifiers with different class members like variables, method and constructors.

Access modifiers : access + modify ==> if we look into english meaning it says if you have to modify something that has been already defined or declared then first you need to access that means first you have to reach to that then only you can modify.
Access modifiers control the visibility of the members to which these are attached.And to control this visibility according to the requirement java has provided us four types of access modifiers. 

We can use the combination of access modifiers with non-access modifiers, for this there are different scenarios with some restriction that i will explain in upcoming posts.
But before using the access modifiers with different members we should first focus on access modifiers being used with class definitions or declarations.

Why we need to take care of access modifiers being use with the class definitions?
  1. Follow my post rules to consider while writting and saving java source file, you will notice that first thing that comes after writing few lines in source file is to save the file in local disk, so while saving the file which name should be use or which not to use depends on the access modifiers being used with class definition.
  2. To practice if you want to use the main method(public static void main(String[] args) then in which class you should write this main method will also vary on use of access modifiers being used with class definitions, for this also you can follow my post,rules to write and save java source file
  3. Now to use the different members of a class we need to create the object of that class and there can be situation where we need to create the object of class if different part of application, but this object creation is also controlled by the use of access modifier.
  4. To use the different members of the class , first class should be accessible so first come the accessibility of class then the accessibility of its members.
  5. As java is object oriented language so there may be situation where we need to use inheritance concept and extend the class , so inheritance concept will also rely on visibility of class that you want to extend.
As a conclusion of these five point I can say , if any time you face problem regarding the access modifiers then first look for the accessibility of class then only the accessibility of members.If your system can not find the class that you want to use then how you will use that class in different perspective ?

In this series i will explain about all the access modifiers one by one in upcoming post.

No comments:

Post a Comment