- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Public Private Protected Access Modifiers
Access modifiers of Java, are public, private, and protected. Java also defines a default access level. The protected modifier in Java, applies only when inheritance is involved. Let's take a look of these access modifiers available in Java language
Java Default Access Modifier
The access modifier will be default, in case if you doesn't explicitly declare an access modifier for a method, class, or field, etc.
Java public Access Modifier
When you declare a class, method, interface, or constructor, etc as public, then they can be accessed from anywhere, from any other class in the program. In case, if you try to access the public class (present in other/different package), then you have to import that public class.
Java private Access Modifier
When you declare a class, method, interface, constructor, or variable, as private can only be accessed within the class where they are declared. This is the most restrictive access level in Java.
Note - In Java programming, class and interface can't be private
Java protected Access Modifier
When you declare a variable, method, constructor as protected in a superclass, can only be accessed by the subclasses in the other package or any other class within the package of protected member's class.
Java Access Modifier Example
Here is an example program, illustrates the concept and use of access modifier in Java language:
/* Java Program Example - Java Access Modifiers * A simple example program of Java's access modifiers */ class MyTestClass { int a; public int b; private int c; void setc(int temp) { c = temp; } int getc() { return c; } } public class JavaProgram { public static void main(String args[]) { MyTestClass objct = new MyTestClass(); objct.a = 100; objct.b = 200; System.out.println("a is " + objct.a + "\nb is " + objct.b + "\nc is " + objct.getc()); } }
When the above Java program is compile and executed, it will produce the following output:
As you can see in above example, inside the class MyTestClass, a uses default access, which for this example is the same as specifying as public, b is explicitly specified as public. Member c is given private access. This means that it can't be accessed by the code that is outside of its class. So, inside the class JavaProgram, c can't be used directly. It must be accessed through its setc() and getc() public methods.
« Previous Tutorial Next Tutorial »