When we do not mention any access modifier, it is called default access modifier. The scope of this modifier is limited to the package only. This means that if we have a class with the default access modifier in a package, only those classes that are in this package can access this class. No other class outside this package can access this class. Similarly, if we have a default method or data member in a class, it would not be visible in the class of another package. Lets see an example to understand this: Show
Default Access Modifier Example in Java
In this example we have two classes, Test class is trying to access the default method of Addition class, since class Test belongs to a different package, this program would throw compilation error, because the scope of default modifier is limited to the same package in which it is declared. package abcpackage; public class Addition { /* Since we didn't mention any access modifier here, it would * be considered as default. */ int addTwoNumbers(int a, int b){ return a+b; } } Test.java package xyzpackage; /* We are importing the abcpackage * but still we will get error because the * class we are trying to use has default access * modifier. */ import abcpackage.*; public class Test { public static void main(String args[]){ Addition obj = new Addition(); /* It will throw error because we are trying to access * the default method in another package */ obj.addTwoNumbers(10, 21); } } Output: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method addTwoNumbers(int, int) from the type Addition is not visible at xyzpackage.Test.main(Test.java:12) 2. Private access modifierThe scope of private modifier is limited to the class only.
Let’s see an example to understand this: Private access modifier example in javaThis example throws compilation error because we are trying to access the private data member and method of class ABC in the class Example. The private data member and method are only accessible within the class. class ABC{ private double num = 100; private int square(int a){ return a*a; } } public class Example{ public static void main(String args[]){ ABC obj = new ABC(); System.out.println(obj.num); System.out.println(obj.square(10)); } } Output: Compile - time error 3. Protected Access ModifierProtected data member and method are only accessible by the classes of the same package and the subclasses present in any package. You can also say that the protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes. Protected access modifier example in JavaIn this example the class Test which is present in another package is able to call the package xyzpackage; /* We are importing the abcpackage * but still we will get error because the * class we are trying to use has default access * modifier. */ import abcpackage.*; public class Test { public static void main(String args[]){ Addition obj = new Addition(); /* It will throw error because we are trying to access * the default method in another package */ obj.addTwoNumbers(10, 21); } }2 method, which is declared protected. This is because the Test class extends class Addition and the protected modifier allows the access of protected members in subclasses (in any packages). Addition.java package abcpackage; public class Addition { protected int addTwoNumbers(int a, int b){ return a+b; } } Test.java package xyzpackage; import abcpackage.*; class Test extends Addition{ public static void main(String args[]){ Test obj = new Test(); System.out.println(obj.addTwoNumbers(11, 22)); } } Output: 33 4. Public access modifierThe members, methods and classes that are declared public can be accessed from anywhere. This modifier doesn’t put any restriction on the access. Which of the following describes the protected access specifier? (a) The variable is visible only outside inside the block (b) The variable is visible everywhere (c) The variable is visible to its block and to it’s derived class (d) The variable is not visible to its block What is an protected access specifier?Remarks. The protected keyword specifies access to class members in the member-list up to the next access specifier ( public or private ) or the end of the class definition. Class members declared as protected can be used only by the following: Member functions of the class that originally declared these members.
Which among the following best describes the protected specifier?1. Which among the following best describes the protected specifier? Explanation: The members which are made protected, are most secure if inheritance is not used. But, this facility is provided to keep those members private and with that, they can be inherited by other classes.
Which statement is not true about protected access specifier?Explanation: The protected access modifier is accessible within package and outside the package but only through inheritance. The protected access modifier can be used with data member, method and constructor. It cannot be applied in the class.
Which of the following statement about access modifiers in Java is true?Q 9 - Which of the following is true about protected access modifier? A - Variables, methods and constructors which are declared protected can be accessed by any class.
|