Access Modifiers In Java :
Access modifiers in
java are used to control the visibility of a field, method, class and
contrustor. There are 4 access modifiers in java. They are : 1). Private 2). Default or Package 3). Protected 4). Public
Let’s discuss these access modifiers one by one.
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,
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't be used outside the class
//a.methodOfClassA(); Private method can't be used outside the class
//A.B b = new A.B(); Private inner class can't be used outside the class
}
}
private
class
A
{
//Outer class Can not be private
}
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.
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.
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.
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 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 sub class
A a =
new
A();
//System.out.println(a.i); Protected field can't be used outside the package
//a.methodOfClassA(); Protected method can't be used outside the package
//A.B b = new A.B(); Protected inner class can't be used 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.
Above concepts can be summarized like below,
Access Modifier | Usage or Access or Visibility | Inheritance |
private | Within Class Only | Can not be inherited |
Default or No-Access Modifier | Within Package Only | Can be inherited to sub class within package |
Protected | Within Package Only | Can be inherited to any subclass |
Public | Anywhere | To any subclass |
No comments:
Post a Comment