Access modifiers

Access modifier in Java programming

Access modifier in Java programming

In this tutorial, we will discuss Access modifier public private protected in Java programming language.
Access modifier controls the access of a Class and, Methods, Constructor and data members from another class.
In Java language, there are four access modifiers

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

Public access modifier

In Java, class methods constructor data members are declared as public and can be accessed from anywhere.
This modifier does not prevent access

Example of Public access modifier in Java
Program 1

//save this program name as TestJava 
class TestJava{ 
public static void main(String args[]){ 
Find_Total sum=new Find_Total(); 
sum.add(56,42,43);//call method }
 }
//save this program name as Find_Total 
class Find_Total{ 
public int add(int a, int b, int c){ //method declared as public 
System.out.println("Total is"+(a+b+c)); 
return a+b+c; }
 }

Compile and run
javac FindTotal.java
java FindTotal

When the above code is executed, it produces the following results:

Total is :141

 

Private access modifier

Private access modifiers are declared using the private keyword.
The methods or data members declared as private is accessible only within the class. Private does not allow access from any other class.
Classes or interfaces cannot be declared as private.

class Private_Ex{ 
private int speed=70;//variable declared as private 
private void calc_Speed()//method declared as private 
{ System.out.println("Car speed is "+speed); 
} 
} 

class vehicle_car{ 
public static void main(String args[])
{ 
Private_Ex s=new Private_Ex(); 
s.calc_Speed(); }//call the private method from outside class
 }

 

When the above code is executed, it produces the following results:

compile time error

In the above program, we have two classes Private_Ex, Vehicle_car. Private_Ex class contain the private variable and private method. we try to access the private class members from outside the class.

There is compile time error

 

class Student{
private int marks=78;//private variable
private Student()//private constructor
{
System.out.println("Student amrks is "+marks);
}
}
class School{
public static void main(String args[]){
Student s=new Student();//create object to call constructor
}
}

When the above code is executed, it produces the following results:

compile time error

In the above program, we have two classes Student, School. Student class contain private variable and the private constructor. we try to access the private class members from outside the class.

When a constructor is declared as private therefore we can not create an object of that class from outside of class.

There is compile time error

 

Protected access modifier

Protected access modifier is used to access the same packages including many classes.

package one;
class Employee{
protected void emp_one()
{
System.out.println("Employee name is sanith");
}
}
package two; 
class Company{ 
public static void main(String args[]){ 
Employee e=new Employee(); 
e.emp_one(); 
}
 }

 

When the above code is executed, it produces the following results:

compile time error

 

Example 2

package pack;
public class Employee1{
protected void emp_one()
{
System.out.println("Employee name is sanith");
}
}

package mypack;
import pack.*;
class Company1 extends Employee1{
public static void main(String args[]){
Company1 e=new Company1();
e.emp_one();
}
}

When the above code is executed, it produces the following results:

Compile time error

 

Default access modifier

when you don’t use any modifier, it is taken as default .the default modifier is accessible only within the package

package 1

//saved by Default_Ex1.java
package pack1;
class Default_Ex1{
void display(){
System.out.println("Default data memebers");
}
}

package 2

//saved by Default_Ex2.java
package mypack;
import pack1.*;
class Default_Ex2{
public static void main(String args[]){
Default_Ex1 obj=new Default_Ex1();//create object
obj.display();//call method
}
}
compile time error

In the above program, we have two packages  pack1, mypack. pack1 package contains a default method . we try to access the private class members from outside the package mypack.

it generates compile time error

 

Access modifiers in Java

Suggested for you

Usage of Static in Java programming language
The instanceof keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago