Access Modifiers in Java.

 

The access modifiers in Java helps to restrict the scope of a class, constructor, variable, method, or data member.

There are four types of access modifiers in java.

  1. Default
  2. Private
  3. Protected
  4. Public

 

 

 

1.Default access modifier

When we don’t use any access modifier for a class, method, or data member, it is treated as a default access modifier by default.

Access modifier is accessible only within the same package, It cannot be accessed from outside the package.

 

Simple Example of Default access modifier

//Java program to illustrate default modifier 
package techPackage;

/* Since we didn't mention any access modifier here, it would
* be considered as default.
*/
class Student { 
void reading() {
System.out.println("Student reading");
}
}

package roundPackage;

import techpackage.*;

class Test {
public static void main(String args[]) {
Student s = new Student();
s.reading();
}
}

Output:

Compile-time error

 

In the first Student class, we didn’t mention any access modifier that it is considered as default. In the second Test

class, we are importing the roundPackage package, but still, we will get an error, because the class we are trying to use has a default access modifier.

In this program, we are getting the error because we are trying to access the default method in another package.

 

2. Private access modifier

The scope of private modifier is accessible only within the class. It is specified using the keyword private.

Private data members and methods are only accessible within the class.

ParentClass Classes or interface can not be declared as private if we declared than getting compile-time error.

If any class has a private constructor, then we can not create the object of that class from outside of the class.

 

Simple Example of Private access modifier

class Student {
private int rolleNo = 101;

private void reading() {
System.out.println("student is reading");
}
}

public class Test {
public static void main(String args[]) {
Student s = new Student();
System.out.println(s.rolleNo);// Compile Time Error
s.reading();// Compile Time Error
}
}

Output:

Compile-time error

 

I the above example, we are getting a compilation error. because we are trying to access the private data member and private method of class Student in the class Test.

 

3.Protected Access Modifier

The protected access modifier is accessible within the class, within the same package, outside package by subclass only. It is specified using the keyword protected.

The protected access modifier is not applied to the class, it is applied to a data member, method, and constructor only.

 

Simple Example of Protected access modifier

//Java program to illustrate 
//protected modifier 
package techPackage;

public class College {
protected void display() {
System.out.println("Technicla Round");
}
}


//Java program to illustrate 
//protected modifier 
package roundPackage;

import techPackage.*; //importing all classes in package techPackage

//Class Student is subclass of College 
class Student extends College {
public static void main(String args[]) {
Student s = new Student();
s.display();
}
}

Output:

Technical Round

 

4.Public Access Modifier

The public access modifier is accessible from everywhere in the program. It is specified using the keyword public. There is no restriction on the public.

 

Simple Example of Protected access modifier

//Java program to illustrate 
//public modifier 
package techPackage;

public class Student {
public void reading() {
System.out.println("Technical Rond");
}
}

package roundPackage;

import techPackage.*;

class Test {
public static void main(String args[]) 
{ 
Student s = new Student(); 
s.reading(); 
}
}

Output:

Technical Round

 

Important conclusions Point.

1. The modifiers are applicable for inner classes but not for outer classes, are Private, Protected, Static.

2. The modifiers are applicable for classes but not for the interface is final.         

3. The modifiers are applicable for classes but not for the enums is final and abstract.      

4. The modifiers are applicable for methods and which we can’t use anywhere else native.   

5. The only modifiers which are applicable for contractors are  Public, Private, Protected, default.

6. The only application modifier for a local variable is final.                                       

 

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

53 Replies to “Access Modifiers in Java.”

  1. Thanks for your whole work on this website. My niece takes pleasure in working on research and it is simple to grasp why. We all notice all relating to the powerful ways you render vital tricks via your website and therefore welcome response from website visitors on this topic while our own simple princess is without question understanding so much. Take pleasure in the rest of the year. Your performing a wonderful job.

  2. I am glad for writing to make you know what a magnificent encounter my wife’s girl had studying your web site. She mastered several pieces, which include what it’s like to possess an awesome helping nature to let other individuals very easily know precisely some multifaceted subject matter. You truly exceeded people’s expectations. Thank you for presenting these powerful, trusted, informative and in addition fun tips about this topic to Kate.

  3. I am only writing to make you understand what a remarkable experience our princess undergone viewing your webblog. She picked up some things, which included what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty know precisely some problematic subject matter. You truly exceeded people’s expectations. Thank you for producing these practical, trusted, informative and in addition fun tips about this topic to Kate.

  4. I’m also commenting to let you be aware of of the brilliant discovery my child encountered checking the blog. She came to understand a lot of details, including how it is like to have a marvelous coaching character to get folks completely fully grasp a variety of complex issues. You actually did more than her desires. I appreciate you for coming up with such essential, dependable, edifying and cool guidance on that topic to Ethel.

  5. I precisely desired to say thanks again. I’m not certain the things I could possibly have carried out in the absence of the creative concepts discussed by you concerning my area of interest. Entirely was a daunting concern for me, but coming across a expert avenue you handled it forced me to cry for contentment. I’m grateful for the assistance and as well , expect you comprehend what a powerful job that you are carrying out educating many people by way of a web site. I am certain you’ve never come across all of us.

  6. My spouse and i got really joyous that Jordan managed to do his reports through your precious recommendations he had through the web pages. It is now and again perplexing to just find yourself handing out secrets and techniques which people might have been trying to sell. We really understand we now have you to thank for this. Those illustrations you made, the straightforward website navigation, the relationships your site make it possible to promote – it is mostly wonderful, and it is making our son in addition to us understand this topic is thrilling, which is certainly wonderfully vital. Thanks for the whole thing!

  7. I precisely had to say thanks all over again. I’m not certain the things I could possibly have created in the absence of the actual creative ideas discussed by you concerning such concern. It absolutely was a real depressing concern for me personally, but considering a new professional fashion you handled it forced me to cry for delight. I’m grateful for the assistance and as well , expect you comprehend what a powerful job that you are carrying out educating men and women by way of a web site. I am certain you’ve never come across all of us.

  8. I am only writing to make you understand what a remarkable experience our princess undergone viewing your webblog. She picked up such a lot of things, with the inclusion of what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tortuous topics. You undoubtedly surpassed readers’ expected results. Thanks for rendering those useful, trustworthy, revealing as well as unique tips on your topic to Sandra.

  9. I simply wanted to thank you so much once more. I do not know what I would’ve taken care of without these suggestions shared by you regarding that subject. It was an absolute intimidating problem in my position, nevertheless taking a look at this specialized style you solved the issue took me to leap with happiness. I will be happy for your service and thus sincerely hope you really know what an amazing job you have been putting in teaching some other people using your webblog. Most likely you haven’t got to know any of us.

  10. I am just writing to make you understand what a notable experience my wife’s girl had using your web site. She mastered so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn some problematic things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth.

  11. Thanks for your whole work on this website. My mum takes pleasure in setting aside time for research and it is simple to grasp why. We notice all relating to the powerful means you deliver great information on the web blog and as well encourage contribution from other people on that concept plus our daughter is actually being taught a lot. Have fun with the remaining portion of the new year. You’re the one conducting a glorious job.

Leave a Reply

Your email address will not be published.