Study and learn Interview MCQ Questions and Answers on Java Access Modifiers. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Access Modifiers before reading these objective questions.
Java provides 4 access modifiers namely public, private, protected, and default(not a keyword).
Yes. If you do not specify any access modifier before a variable or method, it is considered to be declared under the default access modifer.
A variable or a method marked PUBLIC is available to all outside classes irrespective of package that a class lives in. So, PUBLIC is the least restrictive access modifier in Java.
A variable or method marked PRIVATE is accessible only within the class. Outside classes can not access these. So, PRIVATE is the most restrictive access modifier in Java.
No. A method, variable or class can be marked with only one access modifier.
class ABC { }
private protected class ABC { }
protected public class ABC { }
protected private class ABC { }
Class ABC has a default access modifier. It is not mentioned as it does not have a keyword. You can not combine more than one access modifiers in Java.
No. You can not access a default member from outside the package.
Protected variables and methods must be called from inside subclasses or subclass objects only.
package apartment1; public class Class101 { public String colour = "BLUE"; } package apartment2; import apartment1; public class Apartment2Flat102 { public static void main(String[] args) { Class101 flat = new Class101(); System.out.println(flat.colour); } }
You should import either a specific Class or all Classes of a Java Package. So, use import apartment1.*; or import apartment1.Class101; error: Class101 cannot be resolved to a type
package package1; public class Forest { int area = 10;//sq. km } package package1; public class SmallForest { public static void main(String[] args) { Forest fr = new Forest(); System.out.println("Area = " + fr.area); } }
The DEFAULT variables or methods of class can be called inside any class of the same Package.
package package1; public class Bug { int legs = 4; } package package2; import package1.Bug; public class BugTest extends Bug { public static void main(String[] args) { Bug bug = new Bug(); System.out.println("Bug Legs = " + bug.legs); } }
You can not access a DEFAULT variable or method of a class from outside package in any means even through inheritance. So, the compiler throws error.
Unresolved compilation problem: The field Bug.legs is not visible
package package1; public class Parent { protected void showProperty() { System.out.println("10 million"); } } package package2; import package1.*; public class Children extends Parent { void show() { showProperty(); } public static void main(String[] args) { Children chil = new Children(); chil.show(); } }
Through inheritance, you can access a PROTECTED variable or method of a class even from outside the package. Here, we accessed Parent Class of Package1 from Children Class of Package2.
package package1; public class Factory { private void run() { System.out.println("Running factory"); } } package package1; public class FactoryTest { void callFactory() { Factory fac2 = new Factory(); fac2.run(); } public static void main(String[] args) { Factory fac1 = new Factory(); fac.run(); } } a
Running factory Running factory
Running factory
PRIVATE variables or methods of a Class are visible only within that Class. No other class can refer to these PRIVATE things even through inheritance. So, the compiler produces error.
Unresolved compilation problem: The method run() from the type Factory is not visible
package package1; public class Milk { protected int volume = 1000; //Liters } package package2; import package1.Milk; public class RoseMilk extends Milk { public static void main(String[] args) { Milk object = new Milk(); System.out.println(object.volume); } }
You can access PROTECTED things from within the same package in either STATIC or NON-STATIC methods. But, if you want to call PROTECTED things from outside PACKAGE, you should move the code to a non-static method.
package package1; public class Plant { protected void showHeight() { System.out.println("50 meters"); } } package package2; import package1.Plant; public class Tree extends Plant { public static void main(String[] args) { Tree tree = new Tree(); tree.showHeight(); } }
Here, a PROTECTED method of super class has been accessed using Subclass reference. So, the compiler does not produce error even though, it has been called inside STATIC method. If you use Superclass reference or object to access PROTECTED things, you will get error.