Table of Contents
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:
We can use only public access modifiers of class in Java and not create the protected, private class.
| Access modifier | Within class | Within package | Outside packages with sub class only | Outside package |
|---|---|---|---|---|
| Private | Y | N | N | N |
| DEfault | Y | Y | N | N |
| Protected | Y | Y | Y | N |
| Public | Y | Y | Y | Y |
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.
When we assign variables and methods as private, the above error message is displayed.
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
program 2
When we execute the above program, it produces the following results.
marks is:80
student name is mathan
When we execute the above program, it produced the following results.
marks is:80
student name is kantharuban
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.