Thursday, October 18, 2018

Access Modifiers In Java

Hi! friends, In here I am going to describe  access modifiers ,access levels and somewhat about package hierarchy in java.

There are 3 access modifiers in Java.
1.public
2.protected
3.private

Also there are 4 access level in Java.

1.public - with public keyword
2.protected - with protected keyword
3.default - no keyword here
4.private - with private keyword



 According to above image , "public " can use within the class, within the package , outside package by sub class only and outside package.

"private" can only use within the class

"default " can use within class and package.

"public" can use in above all places.

These things can easily understand with variables and methods...

Package Hierarchy

A package can exist inside another package.
According to this case there are 3 packages exist.
  - pack1
  -pack1.pack2
  -pack1.pack2.pack3

If we getting only packages as pack2 and pack3 they are not packages.We should use complete name of them as above.

ex: class A presents in the package pack1
      class B presents in the package pack1.pack2




package pack1;
class A{
public static void main(String[]args){
System.out.println("A");
}
}


package pack1.pack2;
class B{
public static void main(String[]args){
System.out.println("B");
}
}

package pack1.pack2.pack3;
class C{
public static void main(String[]args){
System.out.println("C");
}
}


No comments:

Post a Comment