Home ProgrammingJava Java Modifiers
Java Modifiers

Java Modifiers

by Log Raj
812 views

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

package pack1;
public class ClassA {
 private int x;
 int y; 
 protected int z;
 public int a;
 void meth(){
 System.out.println("Same Class Privte x = "+ x);
 System.out.println("Same Class default y = "+ y);
 System.out.println("Same Class Protected z = "+ z);
 System.out.println("Same Class public a = "+ a);
 }
}

MainClass.java

package pack1;
import pack2.*;
public class MainClass {	
	void meth(){
		ClassA a=new ClassA();
		System.out.println("Same package default a = "+ a.y);
		System.out.println("Same package protected a = "+ a.z);
		System.out.println("Same package public a = "+ a.a);
	}
	public static void main(String[] args) {
	
	System.out.println("------From MainClass--------");
	new MainClass().meth();
	System.out.println("------From ClassA--------");
	new ClassA().meth();
	System.out.println("------From ClassB--------");
	new ClassB().meth();
	System.out.println("------From ClassC--------");
	new ClassC().meth();
	System.out.println("------From ClassD--------");
	new ClassD().meth();	
	}
}

ClassB.java

package pack1;
public class ClassB extends ClassA {
	void meth(){
		ClassA a=new ClassA();
		a.z=20;
		System.out.println("Child class in Same package default a = "+ a.y);
		System.out.println("Child class in Same package protected a = "+ a.z);
		System.out.println("Child class in Same package public a = "+ a.a);
		System.out.println("Protected of super class a = "+ z);	
	}
}

ClassC.java

package pack2;
import pack1.*;
public class ClassC extends ClassA {
	public void meth(){
	ClassA a=new ClassA();	
	System.out.println("Child class in different package public a = "+ a.a);
	System.out.println("protected of superclass but diffe pakage"+z);
	}
}

ClassD.java

package pack2;
import pack1.ClassA;
public class ClassD {
	public void meth(){
		ClassA a=new ClassA();
		System.out.println("In different package public a = "+ a.a);
	}
}

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.

You may also like

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Modifiers

by Log Raj time to read: 4 min
0