Call: +91-9992448483| Email: admin@pyarb.com

Java Modifiers


Java Modifiers

Java Modifiers are keywords that are added with definitions of class, method and member variables to extend their meaning. There are total 9 Java Modifiers and we can divide these Java Modifiers in two different categories.

  • Access control modifiers
  • Non-access modifiers

In this post we will discuss about Java access modifiers. There are 4 access modifiers in java. These are:-

  1. Private access modifier:

We can apply private access control by using private keyword. Private access modifier is most restrictive access modifier in java. Private access modifier is only allowed with methods and member variables means we cannot apply private keyword with classes and interfaces. All the methods and variables defined with keyword private are known as private members of class. Private members can only be used inside that class means if a member is private than we cannot use that member outside the class.

  1. Default access modifier:

There is no explicit modifier to apply default level access modifier so all the members without any access modifiers will have default level access. This is also called package level access modifier because we can use default members with in the package. A class, method and member variable can have default access modifier. Default members or class is only visible within that package.

  1. Protected access modifier:

We can apply protected access level by using protected keyword. Protected access modifier is only allowed with methods and members variables i.e. we cannot apply protected keyword with classes and interfaces. Methods and variables inside interfaces cannot be protected because methods in interfaces are by default public. Protected member is visible either inside the same package or inside sub class in different package there for protected members are not visible non sub classes outside the package.

  1. Public access modifier:

We can apply public access control by using public keyword. Public keyword can be used with class, interface, methods, or variables. Public is least restrictive access control because public class, interfaces or members are visible to any package in the application.

Example code:

Lets have a look at the following code. In this code There are 5 classes.

  • ClassA for Java Modifier Illustration
  • ClassB – subclass of ClassA in the same package
  • MainClass- Class with main method in same class
  • ClassC- subclass of ClassA in the different package
  • ClassD- Class in different package

ClassA.java

MainClass.java

ClassB.java

ClassC.java

ClassD.java

Output:


——From MainClass——–
Same package default a = 0
Same package protected a = 0
Same package public a = 0
——From ClassA——–
Same Class Privte x = 0
Same Class default y = 0
Same Class Protected z = 0
Same Class public a = 0
——From ClassB——–
Child class in Same package default a = 0
Child class in Same package protected a = 20
Child class in Same package public a = 0
Protected of super class a = 0
——From ClassC——–
Child class in different package public a = 0
protected of superclass but diffe pakage0
——From ClassD——–
In different package public a = 0

Java Modifiers Output
Java Modifiers Output

Conclusions:

  • privates can only be accessed in same class
  • Access default fields and methods in any class of same package
  • Access protected fields in any class of same package and Sub class in any package
  • Access public fields an methods inside any class of any package
Java Access Modifiers
Java Access Modifiers

Tips:

The protected members of class are inherited to any sub class regardless of their package. But you can not access the protected fields by using instance of super-class. In above example In side ClassC, new ClassA().z is not visible But simply z field is inherited.

If you are still confused comment down.

Why main method in java is public?

Answer:

main method is entry point of the program so it is called first. The main() method is called by jvm so it need to be public otherwise jvm will not find main method and throw NoSuchMethodError and program will not start. We define main method with public Java Modifier to make it visible to jvm, that is only reason.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *