Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Access Modifiers in Java

  • In Java, the access modifiers define the scope or accessibility of a field, method, constructor, or class. We can change a field, constructor, function, or class's access level by using the access modifier.


Table Of Content

  • Access Modifiers in Java
  • Access Modifiers Chart
  • Private Access Modifiers
  • Default Access Modifiers
  • Protected Access Modifiers
  • Public Access Modifiers






There are four types of Java access modifiers:

  • Private: Access levels for a private modification depend on the class. Users outside the class cannot access it.
  • Default: The default access modifier's access level is fix to inside the package. It is not accessible from the outside of the package.
  • Public: A public access modifier's access level can be found everywhere. It is accessible from any where form both inside and outside of the classes as well as from both inside and outside of the package.
  • Protected: A protected access modifier has access to the inside and outside the package using a child class. If in case no child class is declared, it cannot be accessed from outside the package.




Java Access Modifiers

Access Modifier Same Class Within package Same package by subclass Outside package bysubclasses Global
Public

YES

YES

YES

YES

YES

Protected

YES

YES

YES

YES

NO

Default

YES

YES

YES

NO

NO

Private

YES

NO

NO

NO

NO



Private


Private access modifier can be accessible within class.

  • Only the class in which they are declared can access methods or data members that have been declared as private.
  • The members of these classes are not accessible to any other classes in the same package.
  • Top-level classes and interfaces cannot be declared as private or protected because they can only be seen by members of the enclosing class and any subclasses, respectively.



 // example of private access modifier  
package HELLO;
class Age{  
	private int Age=40;  
	private void DisplayAge(){
		System.out.println("My age is"+Age);
	}  
}    
public class Main{  
 public static void main(String args[]){  
   Age age=new Age();  
   System.out.println(age.Age);   //C.T  Error  
   age.DisplayAge();             //C.T  Error
   }  
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The field Age.Age is not visible 
The method DisplayAge() from the type Age is not visible 

Private Constructor


You cannot create an instance of a class from outside the class if the constructor is made private. see below example

 // example of Private Constructor  
package HELLO;
class Age{  
	private int Age=40;  
	private Age() { // Private Constructor
		// TODO Auto-generated constructor stub
	}
	private void DisplayAge(){
		System.out.println("My age is"+Age);
	}  
}    
public class Main{  
 public static void main(String args[]){  
   Age age=new Age();  //C.T  Error  as Private Constructor
   System.out.println(age.Age);   //C.T  Error  
   age.DisplayAge();             //C.T  Error
   }  
}



Output:

The constructor Age() is not visible 
The field Age.Age is not visible 
The method DisplayAge() from the type Age is not visible 


Default


A class, method, or data member is said to have the default access modifier by default when no access modifier is specified for it.


The data members, classes, or methods that are not declared with any access modifiers are accessible within the same package, It is like default access modifiers


Example of default access modifier
//PackageCheck.java

package tutorial;
 class PackageCheck{
   public void Print(){
      System.out.println("Hello World");
    }
}




class PackageCheck is not accessible in Main.java


//Main.java


package HELLO;
import tutorial.*;
class Main {
    public static void main(String[] args){
    	PackageCheck packageCheck=new PackageCheck();  //C.T  Error
    	packageCheck.Print();  //C.T  Error
   }
}


Protected


The only way to access the protected access modifier is through inheritance, both inside and outside of the package.

The constructor, method, and data member can all be used with the protected access modifier. It is not permitted to be used in class.It is more accessible than the default modifier.




Example of protected access modifier
//PackageCheck.java

package tutorial;
    public class PackageCheck{
    protected  void Print(){
    System.out.println("Hello World");
    }
}

//Main.java


package HELLO;
import tutorial.*;
class Main {
    publicstatic void main(String[] args){
    PackageCheck packageCheck=new PackageCheck();  
    packageCheck.Print();  
    }
}



Output:

Hello World 


Public


Everywhere has access to the public access modifier. Among all the other modifiers, it has the broadest range.

  • Out of all the access modifiers, the public access modifier has the most flexibility.
  • The program can access any class, method, or data member that has been declared public. The range of public data members is unrestricted.

Example of Public access modifier
//PackageCheck.java

package tutorial;
     public class PackageCheck{
        public  void Print(){
        System.out.println("Hello World");
        }
    }
    



//Main.java


package HELLO;
import tutorial.*;
class Main {
    public static void main(String[] args){
    PackageCheck packageCheck=new PackageCheck();  
    packageCheck.Print();  
    }
}

Output:

Hello World 



Java OOPs Miscellaneous Topic Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Encapsulation
Java Package
Java Access Modifiers
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.