Access Modifiers In Java
Access modifiers in java are used to control the visibility of a field, method, class and
constructor. There are 4 access modifiers in java. They are : 1). Private 2). Default or
Package 3). Protected 4). Public
1). Private
Usage of Private members :
Private members of a class whether it is a field or method or constructor can not be accessed
outside the class.
Inheritance of Private Members :
Private members will not be inherited to sub class.
Important Note :
1). Class can not be a private except inner classes. Inner classes are nothing but again
members of outer class. So members of a class (field, method, constructor and inner class) can
be private but not the class itself.
2). We can’t create sub classes to that class which has only private constructors.
Look at the below examples,
Ex 1.class A {
private int i;
private void methodOfClassA() {// Private Method
System.out.println(i); // Private field can be used within class
B b = new B(); // Private inner class can be used within class
}
private class B {
// Private Inner Class
}
}
class C extends A {
void methodOfClassC() {
// System.out.println(i); Private member can not be inherited
A a = new A();
//System.out.println(a.i); Private field can not be used outside the
// class
// a.methodOfClassA(); Private method can not be used outside the class
// A.B b = new A.B(); Private inner class can not be used outside the
// class
}
}
Ex 2. private class A
{
//Outer class Can not be private
}
Ex 3.class A
{
private A()
{
//Private Constructor
}
private A(int i)
{
//Private constructor
}
}
class B extends A
{
//Can't create subclass to the class
//which has only private constructors
}
2). Default or Package or No-Access Modifiers
Usage of Default members :
Default members or members with No-Access modifiers are accessed or visible within the
package only. It applies to outer classes also.
Inheritance Of Default Members :
Default members can be inherited to sub classes within package.
Ex 1.
package pack1;
class A
{
int i;
A()
{
//Constructor with default modifier
}
void methodOfClassA()
{
//Method with default access modifier
System.out.println(i);
B b = new B();
}
class B
{
//Inner Class with default access modifier
}
}
class C extends A
{
void methodOfClassC()
{
System.out.println(i);//Default field can be inherited within package
A a = new A();
System.out.println(a.i);//Default field can be used within the package
a.methodOfClassA(); //Default method can be used within the package
A.B b = new A.B();//Default inner class can be used within the package
}
}
package pack2;
//import pack1.A; Class A with default access modifier not visible
outside the package
/*class D extends A Default Class can not have sub class outside the
package
{
void methodOfClassD()
{
System.out.println(i);Default field can not be inherited outside
package
A a = new A(); Can't use constructor with default access modifier
outside the package
System.out.println(a.i); Default field can not be used outside the
package
a.methodOfClassA();Default method can not be used outside the package
A.B b = new A.B(); Default inner class can not be used outside the package
}
}*/
3). Protected
Usage of Protected Member :
Protected member can be used within the package only.
Inheritance Of Protected Member :
Protected Member can be inherited to any sub classes.
Ex 1.package pack1;
public class A
{
protected int i;
protected void methodOfClassA()
{
//Protected method
System.out.println(i); //Protected field can be used within class
B b = new B(); //Protected Inner Class can be used within class.
}
protected class B
{
//Protected Inner Class
}
}
class C extends A
{
void methodOfClassC()
{
System.out.println(i); //Protected field can be inherited to
any sub class
A a = new A();
System.out.println(a.i); //Protected field can be used within
the package
a.methodOfClassA(); //Protected method can be used within the
package
A.B b = new A.B(); //Protected Inner Class can be used within
the package
}
}
package pack2;
import pack1.A;
class D extends A
{
void methodOfClassD()
{
System.out.println(i); //Protected field can be inherited to
any sub class
A a = new A();
//System.out.println(a.i); Protected field can not be used
outside the package
//a.methodOfClassA(); Protected method can not be used outside
the package
//A.B b = new A.B(); Protected inner class can not be used
outside the package
}
}
Important Note :
1). Outer class can not be protected.
2). We can create sub classes to a class which has only protected constructors but we can’t
create objects to that class outside the package.
4). Public
Usage of Public members :
Public members can be used anywhere.
Inheritance Of Public Members :
Public members can be inherited to any sub class.
Ex 1. package pack1;
public class A
{
public int i;
public void methodOfClassA()
{
//public method
System.out.println(i); //public field can be used anywhere
B b = new B(); //public Inner Class can be used anywhere.
}
public class B
{
//public Inner Class
}
}
class C extends A
{
void methodOfClassC()
{
System.out.println(i); //public field can be inherited to any
sub class
A a = new A();
System.out.println(a.i); //public field can be used anywhere
a.methodOfClassA(); //public method can be used anywhere
A.B b = new A.B(); //public Inner Class can be used anywhere.
}
}
package pack2;
import pack1.A;
class D extends A
{
void methodOfClassD()
{
System.out.println(i); //public field can be inherited to any
sub class
A a = new A();
System.out.println(a.i); //Public field can be used anywhere
a.methodOfClassA(); //Public method can be used anywhere
A.B b = new A.B(); //Public inner class can be used anywhere
}
}
Above concepts can be summarized like below,
Access Modifier Usage or Access or Visibility Inheritance
private Within Class Only Can not be inherited
Can be inherited to sub class within
Default or No-Access Modifier Within Package Only package
Protected Within Package Only Can be inherited to any subclass
Public Anywhere To any subclass
Java Interview Questions On Modifiers
1)How many types of modifiers are there in Java.?
Two types of modifiers are there in java. They are,
a) Access Modifiers
b) Non-access Modifiers
2) What are access modifiers in java.?
These are the modifiers which are used to restrict the visibility of a class or a field or a
method or a constructor. Java supports 4 access modifiers.
a) private : private fields or methods or constructors are visible within the class in which they
are defined.
b) protected : Protected members of a class are visible within the package but they can be
inherited to sub classes outside the package.
c) public : public members are visible everywhere.
d) default or No-access modifiers : Members of a class which are defined with no access
modifiers are visible within the package in which they are defined.
3) What are non-access modifiers in java.?
These are the modifiers which are used to achieve other functionalities like,
a) static : This modifier is used to specify whether a member is a class member or an instance
member.
b) final : It is used to restrict the further modification of a class or a method or a field.
c) abstract : abstract class or abstract method must be enhanced or modified further.
d) synchronized : It is used to achieve thread safeness. Only one thread can execute a method
or a block which is declared as synchronized at any given time.
4) Can we use a field or a method declared without access modifiers outside the
package.?
No, we can’t use a field or a method with no-access (default) specifiers outside the package in
which their class is defined.
5) Can a method or a class be final and abstract at the same time.?
No, it is not possible. A class or a method can not be final and abstract at the same time. final
and abstract are totally opposite in nature. final class or final method must not be modified
further where as abstract class or abstract method must be modified further.
6) Can we declare a class as private.?
We can’t declare an outer class as private. But, we can declare an inner class (class as a
member of another class) as private.
7) Can we declare an abstract method as private also.?
No, abstract methods can not be private. They must be public or protected or default so that
they can be modified further.
8) Can we declare a class as protected.?
We can’t declare an outer class as protected. But, we can declare an inner class (class as a
member of another class) as protected.
9) A class can not be declared with synchronized keyword. Then, why we call classes
like Vector, StringBuffer are synchronized classes.?
Any classes which have only synchronized methods and blocks are treated as synchronized
classes. Classes like Vector, StringBuffer have only synchronized methods. That’s why they are
called as synchronized classes.
What Are Access And Non-Access Modifiers In Java?
Java provides two types of modifiers. One is Access Modifiers and another one is Non-Access
Modifiers. Access modifiers are used to control the visibility of a class or a variable or a
method or a constructor. Where as non-access modifiers are used to provide other
functionalities like synchronizing a method or block, restricting the serialization of a variable
etc. In this post, we will discuss what are access and non-access modifiers in java.
Access Modifiers In Java :
As already said, access modifiers are used to control the visibility of a class or a method or a
variable or a constructor. There are 4 different access modifiers are available in java. They
are – private, default (or no access modifier), public and protected.
private : private members of a class, whether it is a field or a method or a constructor, can
not be accessed outside the class in which they are defined. private members are also not
inherited to sub class.
default : default members or members with no access modifier are visible within the package.
And they are inherited to only sub classes which reside in the same package. That means they
are not inherited and visible outside the package.
public : public members are visible everywhere and they are inherited to any sub class.
protected : protected members have half the characteristics of public members and half the
characteristics of default members. i. e protected members are visible within package like
default members and they can be inherited to any sub class just like public members.
Non-Access Modifiers In Java :
Java provides some other modifiers to provide the functionalities other than the visibility.
These modifiers are called Non-Access Modifiers. There are many non-access modifiers
available in java. Each modifier have their own functionality. Some of the most used non-
access modifiers are listed below.
static : The members which are declared as static are common to all instances of a class.
Static members are class level members which are stored in the class memory.
final : This modifier is used to restrict the further modification of a variable or a method or a
class. The value of a variable which is declared as final can’t be modified once it gets a value.
A final method can not be overridden in the sub class and you can not create a sub class to a
final class.
abstract : This modifier can be used either with a class or with a method. You can not
apply this modifier to variable and constructor. A method which is declared as abstract must
be modified in the sub class. You can’t instantiate a class which is declared as abstract.
synchronized : This modifier is used to control the access of a particular method or a block by
multiple threads. Only one thread can enter into a method or a block which is declared as
synchronized.
transient : This modifier is used in serialization process. A variable which is declared as
transient will not be serialized during object serialization.
volatile : volatile modifier is used in multi threaded programming. If you declare a field as
volatile it will be signal to the threads that it’s value must be read from the main memory
rather then their own stack. Because volatile field is common to all threads and it will be
updated frequently by multiple threads.
strictfp : This modifier is used for floating-point calculations. This keyword ensures that you
will get same floating point presentation on every platform. This modifier makes floating point
variable more consistent across multiple platforms.
Access and Non-Access Modifiers In Java :
20 Java Practice Questions On Access Modifiers
1) What is the use of access modifiers in Java?
Answer :
Access Modifiers in Java are used to control the visibility of fields, methods, classes and
constructors.
2) Can you create a sub class to the following class?
class A
{
private A()
{
//First Constructor
}
private A(int i)
{
//Second Constructor
}
Answer :
No, you can’t create sub classes to that class which has only private constructors.
3) Can you find out the error in the below code?
private class A
{
private class B
{
//Inner class
}
}
Answer : Inner classes can be private, but outer classes can not be private.
4) Does field ‘i’ of Class A be inherited to Class B in the below code?
class A
{
protected int i;
}
class B extends A
{
}
Answer :
Yes, protected members of a class are inherited to any sub class.
5) Is the below code written correctly?
class A
{
private class B
{
//inner class
}
}
public class MainClass extends A
{
public static void main(String[] args)
{
B b = new B();
}
Answer :
No. private inner Class B can not be instantiated outside the Class A.
6) Is the below code written correctly?
package pack1;
class A
{
package pack2;
class B extends A
{
Answer :
No. Class with default (no) access modifiers can not have subclass outside the package.
7) Can we declare a class as protected?
Answer :
Yes, but only inner class. Outer class can not be protected.
8) Do you think the below program is written correctly? If yes, what will be the output?
package pack1;
class X
{
protected int i = 1221;
void methodOfX()
{
System.out.println(i);
}
}
public class MainClass
{
public static void main(String[] args)
{
X x = new X();
System.out.println(x.i);
x.methodOfX();
}
Answer :
Yes, it is written correctly. Output will be
1221
1221
9) Why we can’t instantiate Class-A in the below code outside the package even though it
has public constructor?
package pack1;
class A
{
public A()
{
//public constructor
}
}
package pack2;
import pack1.*;
class B
{
A a = new A(); //Compile Time Error
Answer :
Because, Class-A itself has been defined with default access modifier. That means Class-A can
be instantiated within the package in which it is defined. It can not be instantiated outside the
package, even though it has public constructor.
10) Can a protected field of a class be inherited to subclass outside the package?
Answer :
Yes, protected members of a class are inherited to sub classes outside the package.
11) Why Line 17 in the below code is throwing compile time error?
package pack1;
public class A
{
protected A()
{
//protected constructor
}
}
package pack2;
import pack1.A;
class B
{
A a = new A();
Answer :
Because, we can’t instantiate a class outside the package which has only protected
constructors.
12) Do you think the below code compiles successfully even though it is calling super
class’s protected constructor outside the package?
package pack1;
public class A
{
protected A(int i)
{
//protected constructor
}
}
package pack2;
import pack1.A;
class B extends A
{
public B()
{
super(10); //calling super class's protected constructor
}
Answer :
Yes, above code will compile successfully.
13) Can we declare static methods as private?
Answer :Yes, static members of a class can be private.
14) Is the below code written correctly? If yes, what will be the output?
package pack1;
class A
{
protected static String s = "A";
}
class B extends A
{
class C extends B
{
static void methodOfC()
{
System.out.println(s);
}
}
public class MainClass
{
public static void main(String[] args)
{
C.methodOfC();
}
Answer :
Yes, it is written correctly. Output will be A.
15) Write the access modifiers in the increasing order of their visibility?
Answer :
private —> default or no access modifiers —> protected —> public
16) How many public classes a .java file can have?
Answer :
Only one. A .java file can have maximum one public class.
17) What will be the outcome of the below program?
package pack1;
public class A
{
private int methodOne(int i)
{
return ++i;
}
public int methodTwo(int i)
{
return methodOne(++i);
}
}
package pack2;
import pack1.A;
class B extends A
{
int methodOne(int i)
{
return methodTwo(++i);
}
}
public class MainClass
{
public static void main(String[] args)
{
System.out.println(new B().methodOne(101));
}
Answer :
104
18) Can you find out the error in the following code snippet?
class A
{
public void methodOfA()
{
System.out.println("Class A");
}
}
class B extends A
{
@Override
void methodOfA()
{
System.out.println("Class B");
}
}
Answer :
The visibility of methodOfA() has been reduced to default while overriding it in the class B.
You can’t reduce the visibility of a method while overriding it.
19) private method can be overridden as public method. True or False?
Answer :
False. private methods are not at all inherited.
20) A method of super class with default access modifier can be overridden as protected or
public but not as private. True or false?
Answer :True.
21) Monu has written the code like below but it is showing compile time error. Can you
help him to remove the error?
private class A
{
private class B
{
private class C
{
}
}
}
Answer :
Outer class can’t be private. Don’t declare Class A as private.