Access modifiers in Java

 As the name suggests,access modifiers help to restrict the scope of a class,variable,method or data member.There are four types of access modifiers available in java:

    1. Public

    2. Protected

    3. Private

    4. Default: no keyword required


Public access modifier

  • public’ keyword implies making the data member,functions available to everyone

  • All classes in all packages will be able to access the public data members and invoke the public function 

  • It is the least restrictive access modifier



Consider the following program

package OopsusingJava;

public class JavaCourse
{ public int lecturesCompleted;

public void learnJava()

{ System.out.println(“Started Java Programming”);

}

}

And we have another class ‘JavaStudent’ which is inside another package SemesterTwo:

Package SemesterTwo;

Import OopsusingJava.JavaCourse;

public class JavaStudent

{

public void wantToLearnJava()

{ JavaCourse bestJavaCourse = new JavaCourse();

bestJavaCourse.learnJava();

}

}

What will be the output of the program?


Output: Started Java Programming

Inference: Some outside class in a different package is able to access the public function defined in our class


Protected Access Modifier

  • ‘protected’ modifier is more restricted than ‘public’

  • ‘protected’ modifier makes data members/functions available to all classes within the same package and to subclasses (child classes) in different package


Consider the following program


package OopsusingJava;

class JavaCourse

{ protected void display()

{ System.out.println(“Demonstrating protected access modifier”);

}

}

package SemesterTwo;

class JavaStudent extends JavaCourse

{

public void wantToLearnJava()

{ JavaCourse bestJavaCourse = new JavaCourse();

bestJavaCourse.display();

}

}

What will be the output of the program?


Output:

Demonstrating protected access modifier

Inference: Subclass in a different package is able to access the protected function defined in our class


Private access modifier

  • private’ keyword makes the data members/functions available only within the same class in which they are declared

  • These attributes are not even available to classes defined within the same package 

  • Only the methods inside the class can access the private data members


Consider the following program


package cetmca;

public class McaS2

{ private void getStudyMaterial()

{ System.out.println(“You don’t have access”);

}

}

public class JavaStudent

{

public void JavaNotes()

{ McaS2 tutorial = new McaS2();

tutorial.getStudyMaterial();

}

}

Output:

It will throw a compilation error stating not able to access private member of the class McaS2

In case of any situation, where we need to access private data members or any private function then the class has to provide some public function to access that data member or functions


There is no other way through which a private member can be accessed from outside the class

Default access modifier

  • When no keyword is specified with the class member, then the ‘default’ access modifier is used

  • Scope of data members/functions with default access modifier is to all classes within the same package only


Consider the following program  

package cetmca;

public class McaS2

{ void getStudyMaterial()

{ System.out.println(“You don’t have access”);

}

}

public class JavaStudent

{

public void JavaNotes()

{ McaS2 tutorial = new McaS2();

tutorial.getStudyMaterial();

}

}

Output:

You don’t have access

  


Comments

Popular posts from this blog

Enhanced for - loop or for-each loop

Arrays

Constructors