Java modifiers public, private and protected as appropriate for the guidelines of the encapsulation

Today we would like to help you understand the differences between access modifiers: public, private and protected. Java modifiers are very useful – we apply them in order to restrict access to variables and methods inside a class. This way, we can protect elements of the class.

We always add access modifiers at the beginning of a class member definition, like:

 
private int position;
public String description;

Intuitively, we can guess that private modifier will hide the position variable and the public one will expose the description outside the class. But the difference between a variable with protected modifier and a variable without defined access modifier is not so obvious.

The table below presents the properties of a class member after adding an access modifier to it.

public Member visible to everyone (no protection)
protected Member visible only to classes from the same package or to subclasses of the element’s class (limited access)
private Member visible only inside the class (most restrictive)
without access modifier Like protected, but not visible to subclasses outside the package (only to classes from the same package)

The House class provides an example of use of different types of access modifiers. If we try to describe a house in JAVA, intuitively, we would like to make some of the house properties visible to the whole world (the doorbell is supposed to be public and used by everyone who wishes) and to hide others (it is dangerous to make the access code public).

package itcuties.city;

public class House {


	//house dimensions are private variables
	//"it is a property of the house, it cannot be changed by the others"
	private int[] dimensions;
	
	//owner is not visible outside the class and can be change only by using a private method
	//inside the class
	private String owner;
	
	//access code is a protected variable - it is visible only to classes 
	//in the same package or to the subclasses outside the package
	//"we want to give the access code only to friends"
	protected int accessCode;
	
	//doorBell variable is visible to everyone
	//"everyone can use our door bell"
	public Bell doorBell;
		
	//variable without access modifier - like protected, but not visible to
	//subclasses outside the package
	String[] occupantNames;	
	
	//protected method - visible only to classes from the same package or 
	//to the subclasses outside the package("only the family leaving in the house
	//can change the access code to the building")
	protected void changeAccessCode(){
		//...
	}
	
	//public method printing the private variable - a way to show the private member, without
	//exposing it to risk of modification
	public void printOwner(){
		System.out.println("The owner of the house is " + owner);
		
	}
	
	//method visible only inside the class
	private void changeOwner(String newOwner){
		owner = newOwner;
	}
	//nested class representing the bell
	class Bell{
		// ...
	}	

}

One Response to "Java modifiers public, private and protected as appropriate for the guidelines of the encapsulation"

  1. Kristina says:

    Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply to Kristina Cancel reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>