Scientech Easy Access Modifiers in Core java: Where to apply | Scientech Easy

Wednesday, May 16, 2018

Access Modifiers in Core java: Where to apply

In this tutorial, we will learn very important topic Access Modifiers in Core Java. Where to apply Access Modifiers is the main important to understand. I will explain to you in an easy way and point to point so that you do not get any problem. 

Modifiers in Java

There are two types of modifiers in java. First is Access Modifiers and second is Non-Access Modifiers.
Let's see first access modifiers.

Access Modifiers in Java

Access modifiers /specifiers define the boundary for accessing the variable, or methods, or class etc.
       In other words, access modifiers are the modifiers which are used to restrict the visibility of classes, fields, method, and constructors.

Java supports four types of access modifiers.

Access modifiers in core Java, Java access modifiers, Modifiers in Java, Public, Default , Protected, Public access modifiers
Access Modifiers in Java

1. Private
➲ Private access specifier can apply to a variable, method, constructor, inner class but not the outer class that is class itself.
➲ The instance variable can be private but a local variable cannot be private.
➲ Private members (field, method or constructor) cannot be accessed outside the class. that is accessible only within the class.
➲ Private members will not be inherited to subclass.
➲ If we make any constructor as a private, we cannot create the object of that class from other class and also cannot create the subclass of that class.
➲ A class cannot be private except inner classes. Inner classes are the members of the outer class. so members of the class can be private.
Let's understand these points by taking a very simple example.
In this example we create two classes class A and class B. Class A contains private field and private methods. When we will access these private fields and private methods from class B. that is from outside the class, then there will occur compile time error.
Program 1:
    package scientech; class A { // Declaration of Instance Variables private int data=30; // Here instance variable is private. private void msg() // Here method is also private. { System.out.println("Hello Java, this is my first java programe"); } }
Now create another class B and call the class A.
    class B { // Main method public static void main(String[] args) { //Create the object of class A and call members of class A using reference variable 'a'. A a=new A(); // a is a reference variable. System.out.println(a.data): // Compile time error will occur because we cannot call private members of any class from outside the class. a.msg(); // Compile time error. So we cannot call the private method of any class because this is accessible within the class only. } }

Role of Private Constructor

If you declare any constructor of a class as a private then you cannot create the object of the class from outside the class. In other words, we cannot create the subclass of that class. For example.
Program 2:

    class A { // Declaration of Constructor. private A() // Here Constructor is declared as private. { System.out.println("Constructor is private"); } // Declaration of method. private void msg() // Method is private. { System.out.println("Method is private"); } }
Now create another class B and call the class A. As you will call the class A, it will give compile time error.
    class B{ // Main method. public static void main(String args[]) { // Create the object of class A. A a=new A(); // Compile time error. because the constructor is private and we cannot create the object of class A from outside the class. } }
Q. Can you find the error in the code?
       private class Student{
          private int roll=4;
             private class Name{
                // Inner class 
                                         }
                                  }
Ans:- Outer class cannot be private but inner class can be private. Instance variable can be private.

2. Default(Package access modifier)
➲ When access specifier is not specified to members of a class then it is called default access modifier.
➲ Default members are accessed or visible within the same package or folder only. They can not be accessed outside the package.
➲ The default can apply to the instance variable, local variable, constructor, methods, inner class or outer class.
➲ Default members can be inherited to subclass within the package.

 Let's understand with a simple example
 We have created two packages pack1 and  pack2. we are accessing the class A from outside its package since class A is default i.e not public. Therefore it cannot be accessed from outside the package.
Program 3:
    // Save by Student.java package pack1; class Student{ // Declaration of Instance variable. int roll=12; // here instance variable is default. // Declare the method. void name() // Here method has been defined with default access modifier. { System.out.println(" Hello Java"); } }
    // Save by College.java package pack2; import pack1.*; class College{ // Main method. public static void main(String[] args) { // Creating the object of the class Student from pack2. Student obj=new Student(); // Compile time error. because class Student has been defined with default access modifier and cannot be instantiated from outside the package. but if it is public then we can instantiate from outside the package. obj.name(); // Compile time error. because of the Default access modifier. } }
3. Protected(Inherited access modifier) 
➲ It can be applied to the instance variable, local variable, constructor, methods, inner class but not outer class.
➲ Protected access modifier is used within the package and outside the package but through inheritance only.
➲ Protected members can be inherited to subclass.
➲ If we make constructor as protected then we can create the subclass of that class within the same package but not outside the package.

Let's understand with a simple example.
In this example, we have created the two packages pack1 and pack2. The Student class of pack1 package is public so it can be accessed from outside the package. but name method of this package is declared as protected. so it can be accessed from outside the class only through the inheritance.
Program 4:
    // Save by Student.java package pack1; public class Student{ // Declaration i=of Instance variable. int roll=12; // here instance variable is default. protected void name() // Here method has been defined with protected access modifer. { System.out.println("My roll no. is 12"); } }
    // Save by College.java package pack2; import pack1.*; class College extends Student { public static void main(String args[]) { // Creating the object of class Student from pack2. Student obj=new Student(); // Calling the method using reference variable obj. obj.name(); } }
    Output: My roll no. is 12.
4. Public(Universal access modifiers)
➲ It can apply to the instance variable, constructor, inner class, outer class, method but not a local variable.
➲ Public members can be used anywhere.
➲ Public members can be inherited to any subclass. 
Example of public access modifier:
Program 5:

    // Save by Student.java package pack1; public class Student{ int roll=12; // here instance variable is default. public void name() // Here method has been defined with protected access modifer. { System.out.println(" My roll no. is 12"); } }
    // Save by College.java package pack2; import pack1.*; class College extends Student { public static void main(String args[]) { // Creating the object of class Student from pack2. Student obj=new Student(); obj.name(); } }
    Output: My roll no. is 12.
  

Key of access modifier(Always remember)

    Private  >  Default  >  Protected  >  Public
   More restrictive ----------------> Less restrictive.
                                      Decreasing
Final Words: 
We hope this article will help you to understand the topic Access Modifiers in Core Java

More on Access Modifiers:
1. Access Modifers Interview Questions 


Next ➝ Access Modifiers Interview Questions                                                              Scientech Easy loves you 

Popular