Access modifiers helps to restrict the access of our class by other programs.
Even though we don't specify any access modifier, implicitly it has default access.
public, protected and private are the keywords used in access control.
public: By marking a class , method or variable as public , it can be accessed from anywhere irrespective of the packages.However ,though it is public, to use a class residing in some other package , we have to import it in our class.
A class can be marked only as public or default.
A java file is named with the name of the class. If there are more than one class in the java file , then the source file should be named with the class marked as public and this class should have the main method implemented. There cannot be more than one public class in a java source file.If no class is marked public, then the one with main method should be used as the source file(.java) name.
protected: By marking a method or variable as protected, it can be accessed within the class as well its subclass irrespective of the package.It cannot be accessed if there is no inheritance.
default: If there is no access specified then, implicitly compiler applies default access to it.Like other key words, we cannot explicitly specify this modifier.In this case, the class, method or variables are accessible within the class and even from some other class, but within the same package.
private: By marking a method or variable as private, its access is restricted to its class. Nothing from outside the class can access it.
Even though we don't specify any access modifier, implicitly it has default access.
public, protected and private are the keywords used in access control.
public: By marking a class , method or variable as public , it can be accessed from anywhere irrespective of the packages.However ,though it is public, to use a class residing in some other package , we have to import it in our class.
A class can be marked only as public or default.
A java file is named with the name of the class. If there are more than one class in the java file , then the source file should be named with the class marked as public and this class should have the main method implemented. There cannot be more than one public class in a java source file.If no class is marked public, then the one with main method should be used as the source file(.java) name.
protected: By marking a method or variable as protected, it can be accessed within the class as well its subclass irrespective of the package.It cannot be accessed if there is no inheritance.
default: If there is no access specified then, implicitly compiler applies default access to it.Like other key words, we cannot explicitly specify this modifier.In this case, the class, method or variables are accessible within the class and even from some other class, but within the same package.
private: By marking a method or variable as private, its access is restricted to its class. Nothing from outside the class can access it.
No comments:
Post a Comment
Got to say something?...Leave your comments here