java

Access modifier public private protected in Java

Public,private and protected access modifier in Java

The access modifier in java is used to decide the accessibility of scope of a field, method and constructor or class. If you would like change the accesibility of that, you can do that by appling access modifier to it.

There are four  type of Access modifiers  in Java:

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

We can use only public access modifiers of class in Java and not create the protected, private class.

Access modifier

Let's understand the accessibility of access modifier using a simple table
Access modifierWithin classWithin packageOutside packages with sub class onlyOutside package
PrivateYNNN
DEfaultYYNN
ProtectedYYYN
PublicYYYY

private

Private Access modifiers allow accessing of only own class. Private methods and private variables can not be accessed from other classes.
Program 1
Example
//In the above program, We have created two classes Private_Ex and sample.
We can not apply any access modifier in these classes. So it meanes defined as publis
We can access data members any classes//
When we executed the above program, it produced the following results.
34
My exam result is Grade A

Program 2

At this program, variable int age and method void stu_Exam is declared private. So, we can not handle int age from another class.

Example

When we assign variables and methods as private, the above error message is displayed.

protected

Protected Access modifier allows to access within class and its child class. Protected methods and protected variables can not be accessed from other classes .

Program 1

Example
When we execute the above program, it producess the following results.
marks is :70
student name is kannan

program 2

Example

When we execute the above program, it produces the following results.

marks is:80
student name is mathan

Public

When we declare variables and methods as public , we can access them from any of the outside class.


When we execute the above program, it produced the following results.

marks is:80
student name is kantharuban

4. Default
If you don’t use any access modifier of any scope(feild, method, constructure), it is treated as default access modifier. The default access modifier can be accessed only within the packages. It can not be accessed from outside the packages.
Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.