Access modifier in Java programming
- Home
- Access modifiers
- Access modifier in Java programming
- On
- By
- 0 Comment
- Categories: Access modifiers, keyword
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
- public
- private
- protected
- 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