Skip to main content

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. 


    Comments

    Popular posts from this blog

    Assignment of ITA/ Information Technology and Application BCA- Technology369kk

    Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

    C++ and Java Practical All Questions Answers - BCA -Technology369kk

    C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

    Assignment of PMO (Principal of Management and Organization) - Technology369kk

     ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...