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
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes