Access Modifier in java
Skip to main content

Access Modifier in java



access modifiers in java, access modifiers in python, difference between private and protected, modifiers in java, private access modifier, public access specifier, protected modifier in java, default access modifier in java, non access modifiers in java




Access Modifier in java


Access Modifier in java in this tutorial is explain about what is definition of Access Modifier  and types of access modifier like public, private, protected and default modifier with explanation and given example of Java Programming.

 Access Modifier in Java is provide a scope of a fields, methods, constructor and class. there have four type of Access Modifier in Java.

Type of Access Modifier:-

1. Public  2. Private  3. Protected  4. Default

1. Public access modifier:- 

       Public access modifier is used then you access everywhere. its scope it widest area of all other of modifiers. It can be accessed inside the class, outside of the class, within the package and outside of the package.

public class Test{

public void MsgPrint(){

           System.out.println("Hello");

}

}

class Main{

      public static void main(String[] args){

            Test mObj = new Test();

          mObj.MsgPrint();

}

}

Output:-  Hello


Most Read:- Java Keywords using in Java


2. Private acess modifier:- 

        if you use private access modifier with variable, method and constructor then you can access in same class only. you cannot accessed outside of the class and package.

class Test{

private void MsgPrint(){

           System.out.println("Hello");

}

}

class Main{

      public static void main(String[] args){

            Test mObj = new Test();

         

}

}

if you use private modifier with constructor-

class Test{

private Test(){}// Constructor as a private

 void MsgPrint(){

           System.out.println("Hello");

}

}

class Main{

      public static void main(String [] args){

            Test mObj = new Test();  // Compile time error 

           mObj.MsgPrint();  

}

}

3. Protected access modifier:-  

         if you use protected access modifier with variable, method and constructor then you can access within class but outside of the package you can access in his child class.

import package1;

public class Test{

protected void MsgPrint(){

           System.out.println("Hello");

}

}


import package2;

class Main extends Test{

      public static void main(String [] args){

            Test mObj = new Test();

          mObj.MsgPrint();

}

}

Output:-  Hello

3. Default access modifer :- 

        Default modifier if you use then you access in same package only. you use default before variable, method or constructor then you cannot access outside of the package.

import package1;

class Test{

 void MsgPrint(){

           System.out.println("Hello");

}

}


import package2;

class Main{

      public static void main(String [] args){

            Test mObj = new Test();// Error

          mObj.MsgPrint();

}

}


Post a Comment

0 Comments