Loading...

Introduction to Access Modifiers in Java with an Example

Introduction to Access Modifiers in Java with an Example

Java is a high-level programming language and an oops-based concept. When we start learning Java programming, at first its syntax is very confusing for beginners because of the keywords used even in the basic Hello World code. So in the Hello World program, we came across the keyword “public.” It’s an access modifier, an accessibility modifier, or an access specifier, which all say the same thing. So in every class, we have variables, methods, and constructors, each of which has a scope, restriction, or visibility. If we want to define the scope of a class member, it means that it should be accessible for that specific class or the entire package or project, which we can do with access modifiers. 

What are Access Modifiers in java?

The scope or visibility of a class’s methods, constructors, and variables is defined by the access modifier. So these are used to control access mechanisms.

A Real-life Example of Java Access Modifiers

Generally, we use files and folders on our computer to do our work, so by default, they’re generally set to private access modifier so that only the owner can access them. So it provides control over others and can choose what to do when others want to work with it; specificity also comes with it.

Types of Access Modifiers in Java

Generally, we have four types of access modifiers.

  • Public
  • private
  • protected
  • default or no modifier.

Public Access Modifiers in Java

When we create a class, by default it’s going to be public, so the public keyword means anybody can access it. So once we declare it public, I can use it throughout the entire package, other packages, and other projects as well.

package com.modifiers public class Student{ public int roll_no=7; public void print(){ System.out.println(roll_no); } } public class Main{ public static void main(String args[]){ Student s=new Student(); System.out.println(s.roll_no); s.print(); }
Code language: Java (java)

Output:

7 7
Code language: Bash (bash)

looking for the same package but in a different class.

package com.modifiers; public class Teacher{ public class void main(String[] args){ student s=new student(); System.out.println(s.roll_no); s.print(); } }
Code language: Java (java)

Output:

7 7

Let’s look at the outside of the package.

package com.not_modifier_packages import com.modifiers.Student public class Guru{ public class void main(String[] args){ student s=new student(); System.out.println(s.roll_no); s.print(); } }
Code language: Java (java)

Output:

7 7
Code language: Bash (bash)

We learned that public classes can be used within a class, and the same package can be used with different classes and packages. We can also use inheritance (subclassing) within or outside the different packages. So the public is accessible everywhere; it’s a universal modifier.

Default Access Modifiers in Java

Interestingly, we don’t need to specify the modifier by default; it’s going to take that, which is why we call it a no modifier or also package private. A class can be the default modifier. Yes, we can use even the constructor, methods, and variables. When a program runs, it means it is within the same package, which is why it is called a private package; it is also used for subclasses (which should also be within the same package). where the private is accessible only within the class.

class Student{ int roll_no=7; void print(){ System.out.println(roll_no); } public static void main(String args[]){ Student s=new Student();//able to acces within the same class System.out.println(s.roll_no); s.print(); } }
Code language: Java (java)

Output:

7 7
Code language: Bash (bash)

Private Access Modifiers in Java

We can’t use a class as a private modifier because if the class itself is private, then how can we call it? That doesn’t make sense, so make sure it’s public. Well, you can use the data members as a private modifier.

package com.modifiers public class Student{ private int roll_no=7; private void print(){ System.out.println(roll_no); } public static void main(String args[]){ Student s=new Student();//able to acces within the same class System.out.println(s.roll_no); s.print(); } }
Code language: Java (java)

Output:

7 7
Code language: Bash (bash)

Let’s try within the same package but in a different class.

package com.modifiers public class Student{ private int roll_no=7; private String school; private Student(){ school="Bharati School" //private constructor can be also declared } public void print(){ System.out.println(roll_no); } } public class Main{ public static void main(String args[]){ Student s=new Student();//<strong>private constructor can't be used for other classes </strong> s.print();// <strong>gets compile error because private unable to access with outside the class</strong> System.out.println(s.roll_no);//<strong>gets compiler error</strong> } }
Code language: Java (java)

So we can access within the same class, but we can’t access its subclass with a different class or with a different package with any other class. 

Protected Access Modifiers in Java

It works similarly to the default and also comes with an extra feature that we can also access with the subclass with different packages too; that is the only difference between them.

import package com.modifiers; public class student{ protected int roll_no=7; protected void print(){ System.out.println(roll_no); } public static void Main(String args[]){ Student s=new Student();//able to acces within the same class System.out.println(s.roll_no); s.print(); } }
Code language: Java (java)

Output:

7 7
Code language: Bash (bash)

Let’s create a subclass with a different package and then see it.

package com.inheritace; import com.modifiers.Student; public class Subclass extends Student{ Subclass obj=new Subclass(); obj.print(); System.out.println(obj.roll_no); }
Code language: Java (java)

Output:

7 7
Code language: Bash (bash)

Understanding Java Access Modifiers(Chart)

This can be considered a cheat sheet. Whenever you are confused, take a glance at it and understand how it works. Just try to remember what we have discussed and you will get full clarity about it.

Advantages of Access Modifiers in java

By using these modifiers we can protect our data it will give you privacy not letting others see our implementation. So limiting access to certain parts of code will make code efficient and improve performance. Access modifiers make code easier also changes can be seen properly in the code. So making code organizing is a way to improve security to the next level. So following the private access modifier owner can control, and can pass whom they want to share with.

Disadvantages of Access Modifiers in java

The main issue is maintaining the code errors; if developers want to change the code errors, for example, changing a method or class but forgetting to change the access modifier, the changes will be invisible to others. 

Conclusions

So, by using modifiers, we can limit the packages to being inside or outside of the various classes. So it’s treated like a control access mechanism that helps to restrict the scope to only classes, constructors, methods, and data members. Also, provide information about Java to the virtual machine.

Frequently asked questions?

What are the access modifiers in Java?

The scope or visibility of a class’s methods, constructors, and variables is defined by the access modifier. So these are used to control access mechanisms.

What are the 3 access modifiers?

Generally, we consider the main 3 access modifiers which are private, public, and private.

What is an access modifier in Java for example?

So it’s treated like a control access mechanism that helps to restrict the scope to only classes, constructors, methods, and data members. If I take the private access modifier, then we can only use it within the same class, not even in a different class within the same package.

What are access modifiers in OOP?

In OOPS, there are public, private, protected, and default access modifiers.

What is an access modifier in Java, with an example?

It’s a keyword in Java used for the scope of the classes, constructors, data members, and methods.

public class Student{ private int roll_no=7; private void print(){ System.out.println(roll_no); } } //Because I'm using the private print method, I can only print within the same class. 
Code language: PHP (php)

Sharing is caring

Did you like what Nithin Reddy wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far