Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Access modifier public private protected in Java

Posted on October 24, 2017September 25, 2019

Table of Contents

  • Public,private and protected access modifier in Java
  • Access modifier
      • private
      • protected
      • Public

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
Access modifier public private protected in Java
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.

Access modifier public private protected in Java
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

Access modifier public private protected in Java
Example
When we execute the above program, it producess the following results.
marks is :70
student name is kannan

program 2

Access modifier public private protected in Java
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.

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes