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

[wp_table id=6912/]

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

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago