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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.