Access Specifiers :
specifies the access level of
variables, Methods , classes.
the four main access specifiers
in Java are:
·
public
·
private
·
protected
·
default(no keyword)
Package1
├── Class A
└── Class B
Package2
├── Class C
└── Class D
---------------------------------------------------------------------------------------------------
Package-1 Package-2
Class- A class -B Class
-C class- D
---------------------------------------------------------------------------------------------------
public yes yes yes yes
private yes
No No
No
protected yes yes yes(Inheritance) Yes
(Inher)
Default yes yes No No
public:
Variables and methods declared as public
can be accessed from any class, regardless of the package.
private:
Variables and methods declared as private
can only be accessed within the same class and are not accessible outside of
it.
default:
(No specifier) Variables and methods with default access can only be accessed
within the same package and are not accessible from classes in other packages.
protected:
Variables and methods declared as protected
can be accessed within the same package and in subclasses in other packages
through inheritance.
package PackageOne;
public class A
{
//define
public variable pubid=10 ;
public
int a =10;// // public var
public
int pubid = 10;
//define
private variable, prVariable =3.45f
private
float prVariable = 3.45f;// private var
//define
protected variable protCar=34;
protected int protVariable = 34;// protected variable ---> Inheritance
//define
def variable defVariable =50;
//` default defVariable =
50;// Invalid // error
int
defVariable = 50; // def Var
//
if we dont write any access specifiers public, private,protected, then it is
def variable
//
Default var
//
2 Access specifiers are appplicable for variables and Methods, Constr,class
//
Define public method M1()
public
void M1()//// public method
{
System.out.println("Calling
public M1()");
}
public
static void main(String[] args)
{
//
create obj for class -A
A
aref = new A();
System.out.println("acesssing
public variable " + aref.pubid);
System.out.println("acesssing
private variable " + aref.prVar);
System.out.println("acesssing
protected variable "+ aref.protVar);
System.out.println("acesssing
default variable "+ aref.defVar);
}
}
------------------
package PackageOne;
public class B {
public
static void main(String[] args) {
//
create obj for class -A
A
aref = new A();
System.out.println("acesssing
public variable "+ aref.pubid); //10
// System.out.println("acesssing private
variable "+ aref.prVar);
//
error :The field A.prVariable is not visible
//
Private variable can be accesseed inside
the same class but not outside class
//
Private variables canot be accessed from
classes - B,C,D
System.out.println("acesssing
protected variable "+aref.protVar);//34
System.out.println("acesssing
default variable "+ aref.defVar);//50
}
}
--------------------
package PackageTwo;
import PackageOne.A;
public class C extends A
{//extends A
//
get copy of parent class variable from class -A
public
static void main(String[] args)
{
//
TODO Auto-generated method stub
//
create obj for class -A
A
aref = new A();
System.out.println("acesssing
public variable "+ aref.pubid);// accessed any wheere
// System.out.println("acesssing
private variable "+ aref.prVar);
/// canot be accseesed
// System.out.println("acesssing
protected variable "+ aref.protVar); // ?????
// The field A.protCar is not visible
//
Note : if we want to access protected variable from other packages -
//we
must use inhertiance
//
create obj for class -C
C
cref = new C();
System.out.println("acesssing
protected variable "+ cref.protVar);//34
// System.out.println("acesssing
def variable "+ aref.defVar);
//
The field A.defVariable is not visible
//
Default access specifier scope is package level
// def variable - can be accessed inside same
package only but not oustide of the
package
// C class is defined in pakcageTwo
// System.out.println("acesssing
protected variable "+ cref.defVar);//error
}
}
-----------------------------
package PackageTwo;
public class D {
public
static void main(String[] args) {
//
TODO Auto-generated method stub
}
}
HW Declare Public, private , protected, default 'varaibles' in class1 ,
check access level of variables in side Classes - class2,class3,class4..
packageA PackageB
Class1 class2 class3 class4
HW Declare Public, private ,
protected, default 'Methods' in class1
, check
these methods can be accessed from class2,class3 ..?
Access Specifiers in Inheritance
In Java, access specifiers control the visibility and accessibility of members
(variables and methods) of a class, especially when dealing with inheritance.
Here’s how each access specifier behaves in the context of inheritance:
package AccesspecifiersInheritance;
class A
{
public
int pubid;
private
int privateVar;
protected
int protectedVar;
int
defVar;
}
check above variables - can be
accessed in the child class
package Inheritance;
public class
TestaccessspecifiersInheritance extends A
{
// extends A
//
getting parent class variable and parent class methods into child class
public
static void main(String[] args) {
//
create obj for class- TestaccessspecifiersInheritance()
TestaccessspecifiersInheritance
tref = new TestaccessspecifiersInheritance();
System.out.println("public
var" + tref.pubid);
System.out.println("private
var"+ tref.privateVar);//
//
Error :Private variable is not visible in other classses
//
We cannot access private variable in
other classes except Class-A
// private variables -- cannot be inherited as we can access private variable
inside the same class where we declared private
but from out side the class
System.out.println("protected
var"+tref.protectedVar);
System.out.println("default
var"+ tref.defVar);
// if we use inheritance, we can get parent class - public , protected, default
// public
variables , protected , default var's
can be inherited
//
Private var's cannot be inherited
}
}
Note:
FAQ Privates variables are inherited or not ?
No. Private variables cannot be
inherited as we cannot access from outside class
Refer:
https://www.sitesbay.com/java/java-access-modifiers
HW Define some public, private, protected, default -' methods', check we can access these methods in child
class using inheritance?
No comments:
Post a Comment