yt

Header Ads

Chapter -4 What is Access Modifier/Specifier in Java

Access Specifier or Modifiers

What is Specifiers 

  • It is a keyword which is used to provide accessibility of data member(variable) and member function(function) of a class.
  • In another word in loop system to defines scope and visibility(Security) access modifier are defined, In defined the data-members of members function can be access from where or Not.
  • It is also called access modifier.

The public is keyword is an access modifier, meaning that it is used to set the access level for classes, attributes, methods and constructors.

An Example of Public access in case of class. 


// Example of public in class:
public class Accpub{ // public is keyword and it is used in class.
    int x=10;
    void show(){
        System.out.println("Value of x="+x);
    }
}
class Public{
    public static void main(String arg[]){
        Accpub obj = new Accpub();
        obj.show();
    }
}




******Output*****



Value of x=10


Modifiers are divided into two types:

Access Modifiers : In this case the controls access level. 
Non- Access Modifiers :- In this case don't access level, but this provided other functionality.
 

Table of Contents

Types of Access Specifier

Access Specifiers are divided into Four types.

1.Default: Automatically generated without any use access specifiers.

2.Public: No Security, can be access from any where in the program.

3.Private: Full Security, this case can't access from outside the class access inside.

4.Protected: Less Secure, can be access inside the class and relative class.


Default:-

  • It is allow automatically generated without any use access specifiers but given value in my requirement.
  • It is used in Classes Case.
  • The class is only accessible by classes in the same package. This is used when you don't specify a specifiers,  You will learn more about packages in the Packages chapter.
  • Lets an example for better understanding.
  • Well now this-day access version of 17,18... etc javaVer support default. 

1. An Example For Classes Case used public types :


public class Public {             // Here now used public with class
  public static void main(String[] args) {
    System.out.println("Hello Students, I hope are you fine.");
  }
}


******Output*****



Hello Students, I hope are you fine.



2.An Example For Classes Case used Default types :


class Public {         // here now not using public with class
public static void main(String[] args) {
    System.out.println("Hello Students, I hope are you fine.");
  }
}


******Output*****



Hello Students, I hope are you fine.


Access specifiers used in  attributes, methods and constructors, 

Public:-

  • It allows the accessibility of data member and member function to the other classes.
  • public element of a class can be accessed anywhere in the program.
  • In Another Word:- It has no Security, that means public members can be provided to the interface to the external world so can be access from any where in the program.
  • Lets see an example for better understanding. 


2.An Example:


public class Inputdata{     // Some case not work public 1st line.
  public String fname = "Annu";
  public String lname = "Priya";
  public String email = "annupriya21@gmail.com";
  public int age = 19;
}

class Showdata{
  public static void main(String[] args) {
    Inputdata obj= new Inputdata();
    System.out.println("Your Name is:"+ obj.fname + " " + obj.fname);
    System.out.println("Email Id: "+ obj.email);
    System.out.println("Email Id: "+ obj.age);

  }
}




******Output*****


PS F:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java Showdata
Your Name is:Annu Annu
Email Id: annupriya21@gmail.com
Email Id: 19



Private:- 

  • 1.It is used to hide data member and member function from the other classes.
  • 2.private element of a class can be accessed only inside in its own class. 3.private element of a class can not be accessed out of that class.
  • In another word:- It is most important and secure members(private) can be access only inside the class or type of access out-side the class.
  • To hide the data members input the in private area
  • Lets see an example for better understanding. 


    An Example:


    // An Example of Private Specifiers
    class Addition{    
        private int a=10;
        private int b=20;
        private int c=a+b;

        public static void main(String arg[]){
            Addition obj= new Addition();
            System.out.println("Addition of a and b:"+obj.c);
        }

    }


    ******Output*****


    Addition of a and b:30


    Protected:- 

    • It is approximately same as private but it allows the accessibility of data member and member function to the child class.
    • IN ANOTHER WORD:- It's less secure by private, protected members can be access by class member and by the members of derived class, that means we can say that protected members can be access inside the class and relatives class.
    • Protected is used in the case of inheritance.
    • Lets see an example for better understanding. 


    An Example:


    // An Example of Protected Specifiers
    class Mydata {
      protected String fname = "Shailesh";
      protected String lname = "Jaiswal";
      protected String email = "shailesh000@gmail.com";
      protected int age = 20;
    }

    class Shailesh extends Mydata{
      private int graduationYearcom = 2023;
      public static void main(String[] args) {
        Shailesh obj = new Shailesh();
        System.out.println("My Name is: " + obj.fname + " " +obj.lname);
        System.out.println("My Email: " + obj.email);
        System.out.println("My Age: " + obj.age);
        System.out.println("My Graduation Year: " + obj.graduationYearcom);
      }
    }



    ******Output*****


    Addition of a and b:30PS F:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java Shailesh My Name is: Shailesh Jaiswal My Email: shailesh000@gmail.com My Age: 20 My Graduation Year: 2023


    Non Access Specifiers :-

    In this case we use with classes, methods, and attributes cases. There are many place we can use suppose that abstract, final, static many keywords are used this is more we can learn next chapter. 

     An Example of final Keywords:


    // Final  keywords:
    class Demo {
      final int x = 10;
      final double PI = 3.14;

      public static void main(String[] args) {
        Demo obj= new Demo();
        obj.x = 50; // will generate an error: cannot assign a value to a final variable
        obj.PI = 25; // will generate an error: cannot assign a value to a final variable
        System.out.println(obj.x);
      }
    }



    ******Output*****


    blog3.java:336: error: cannot assign a value to final variable x
        obj.x = 50; // will generate an error: cannot assign a value to a final variable
           ^
    blog3.java:337: error: cannot assign a value to final variable PI
        obj.PI = 25; // will generate an error: cannot assign a value to a final variable
           ^
    2 errors



    I HOPE THIS IS HELPFUL FOR YOU THANK YOU. 


    No comments

    Theme images by Dizzo. Powered by Blogger.