You are on page 1of 4

Interview Questions on Access Modifiers

Q) Explain about modifiers in Java?


Modifiers in Java
In Java, to give access limitation, restriction, and permission to the class,
attributes, methods, and constructor we use modifiers.
Types of Modifiers in Java
1) Access Modifier
This type of modifier is used to control the access level of the class,
attributes, methods, and constructor.
2) Non-Access Modifier
This type of modifiers used to restrict the further use of a class, attributes,
methods, and constructor and there is another use also other than controlling
access.

Note:
Default access modifier is special because it doesn’t have an associated
keyword. When no explicit access modifier is specified, the types or
members have default accessibility.
public:
1) This is the least restrictive access modifier which means the widest range
of accessibility, or visibility.
2) When applied to a class, the class is accessible from any classes
regardless of packages.
default:
1) When a class, variable or method has default accessibility, it is accessible
to only classes in the same package.

1
private:
1) Members marked private can't be accessed by code in any class other
than the class in which the private member was declared.
protected:
1) Protected members of a class are visible within the package but they can
be inherited to sub classes outside the package.
final:
1) When used in a class declaration, the final keyword means the class can't
be subclassed.
static:
1) The static modifier is used to create variables and methods that will exist
independently of any instances created for the class.
2) All static members exist before you ever make a new instance of a class,
and there will be only one copy of a static member regardless of the
number of instances of that class.
abstract:
1) An abstract class can never be instantiated.
synchronized:
1) When a method is synchronized it can be accessed by only one thread at
a time.
transient:
1) If you mark an instance variable as transient, you're telling the JVM to
skip (ignore) this variable when you attempt to serialize the object
containing it.
volatile:
1) The volatile modifier tells the JVM that a thread accessing the variable
must always settle its own private copy of the variable with the master
copy in memory.
2) The volatile modifier may also be applied to project managers

2
Q) What is the problem with the below code?
package org.nareshit.accessModifer.protected;
Answer: Invalid package name. protected is a java keyword.
Q) Can an inner class be declared private?
Answer: Yes. The inner class can be private.
Q) Explain access levels of differ access modifiers?
Answer:
Access Levels
Modifier Class Package Subclass (different package) World
Public Y Y Y Y
Protected Y Y Y N
no modifier Y Y N N
Private Y N N N

Q) Does the order of public and static declaration is important in main()


method?:
No. It can be placed in any order except that the return type of any method must
before the method name. In this case, void has to specify before the main method
name.
Q) Can a class be declared private?
No. The outer class cannot be private. We will get a compile time error.
Q) Can a class be protected?
An outer class cannot be protected.
However an inner class can be.
Q) Can an abstract class be final?
No. abstract class must be extended by another concrete class.
Q) Example of final class from Java API.
java.lang.String, java.lang.Math
Q) Can a class be declared as static?
No. A outer class cannot be declared static.
Q) Why do we declare a method as static?
When the method has to be accessed even before the instantiating an object for
the class, we should declare the method as static.

3
Q) What are the constraints on a static method or a static block of code?
A static method should not refer to instance variables without an instance.
Cannot use this operator to refer the current instance.
Q) Can we declare static members in a method?
Methods will have only local variables.
Static variables are class level and cannot reside inside a method.

Q) What are the other applicable modifiers for main() method?


A main() Method is static by default.
It can also be declared final and synchronized.
Q) What happens when a main method declared as synchronized?
When main() declared as synchronized, no two threads can access the main() at
the same time.
Q) Explain strictfp keyword in Java?
The strictfp keyword is used to force the precision of floating point calculations
(float or double) in Java conform to IEEE 754 standard, explicitly. Without using
strictfp keyword, the floating point precision may vary depends on target
platform's hardware and CPU's floating point processing capability. strictfp
restricts floating-point calculations and ensuring same result on every platform
while performing operations in the floating-point variable.
The strictfp keyword can be applied for classes, interfaces and methods.
strictfp keyword cannot be applied on abstract methods, variables or constructors.
strictfp cannot be applied on interface methods and abstract classes.
If an interface or class is declared with strictfp, then all methods and nested types
within that interface or class are implicitly strictfp.
Q) Explain the native keyword in Java?
The native keyword is applied to a Java method to indicate that the method is
implemented in native code using JNI (Java Native Interface).

You might also like