Access specifiers are used to control the visibility of class and class members.
Access specifier can be applied to class members ,data members ,methods and constructor.
1.public
2.private
3.protected
4.default
1.Public
Public classes are visible to rest of the class in the project .A java file can have only one public class .
Name of java file should be same as the oublic class in that file otherwise it results in compile time error.
If a file does not have public class,class can be anything.
2.Private
Private members are visible within same class only hence they cannot be accessed from different classes of a project.
Top level class cannot be private.
Nested class can be private.
If constructor is made private then class can be instanciated from different class.
private members are not accessible to other classes ,even though class is public.
3.Default
Default class is visible to rest of the classes present in the same package thus it has package scope.
4.Protected members are visible to all classes in the same packages and child classes in any packages.
Access specifier can be applied to class members ,data members ,methods and constructor.
1.public
2.private
3.protected
4.default
1.Public
Public classes are visible to rest of the class in the project .A java file can have only one public class .
Name of java file should be same as the oublic class in that file otherwise it results in compile time error.
If a file does not have public class,class can be anything.
2.Private
Private members are visible within same class only hence they cannot be accessed from different classes of a project.
Top level class cannot be private.
Nested class can be private.
If constructor is made private then class can be instanciated from different class.
private members are not accessible to other classes ,even though class is public.
3.Default
Default class is visible to rest of the classes present in the same package thus it has package scope.
4.Protected members are visible to all classes in the same packages and child classes in any packages.
Comments
Post a Comment