Learn Pain Less

HomeOur TeamContact
Java
What is Access Modifiers in JAVA
Pawneshwer Gupta
Pawneshwer Gupta
March 24, 2015
1 min

Table Of Contents

01
Access Modifiers in java with example program
02
Access modifier are of 4 types in java:
03
Default access modifier:
04
Private access modifier:
05
Protected access modifier:
06
Public access modifier in java:
What is Access Modifiers in JAVA

Access Modifiers in java with example program

Access modifier is keyword to change visibility of variable,class, constructor or methods. Access modifier will be used according to our need, how and where we want to use those variable,class, constructor or methods.

Access modifier are of 4 types in java:

  1. default
  2. private
  3. protected
  4. public

Example of access modifier in java:

public class Android{
private void show(){
//some code here.
}
}

Default access modifier:

they are visible to package. To make class, variable, constructor or methods visibility to default then we don’t need to use any keyword.

Example of default access modifier:

class Android{
}

This class Android will be accessible to any class within same package.

Private access modifier:

As its name says, it makes the visibility to class only. Which means if any variable or method of a class have access modifier to private then it will only visible inside its class.

Example of private access modifier:

class Android{
private void show(){
//This is private method and visible inside only this Android class.
}
}

Protected access modifier:

Protected access modifier change the visibility to package and subclass only. Subclass means if class Android extends class Java, and class Java have protected method then ot will be accessible inside Android class.

Example of protected access modifier:

class Java {
protected void run(){
System.out.println("accessed from derived class");
}
}
class Android extends Java{
private void show(){
run(); // protected method of java class called here.
}
}

Public access modifier in java:

Public access modifier change the visibility to the world , which means they are accessible anywhere in the project.

Example of public access modifier in java:

public class Java{
public void show(){
System.out.println("public method called");
}
}

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

startaccess modifiersjava tutorial

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

Working with Files, Folders java
Working with Files, Folders java
March 25, 2015
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media