default access also called package accessibility public access protected access private access

J.E.D.I

10.9 Access Modifiers

When creating our classes and defining the properties and methods in our class, we want to implement some kind of restriction to access these data. For example, if you want a certain attribute to be changed only by the methods inside the class, you may want to hide this from other objects using your class. In Java, we have what we call access modifiers in order to implement this. There are four different types of member access modifiers in Java: public, private, protected and default. The first three access modifiers are explicitly written in the code to indicate the access type, for the fourth one which is default, no keyword is used.

10.9.1 default access also called package accessibility

This specifies that only classes in the same package can have access to the class variables and methods. There are no actual keyword for the default modifier; it is applied in the absence of an access modifier. For example, public class StudentRecord { default access to instance variable int name; default access to method String getName{ return name; } } In this example, the instance variable name and the method getName can be accessed from other objects, as long as the object belongs to the same package where the class StudentRecord belongs to.

10.9.2 public access

This specifies that class members are accessible to anyone, both inside and outside the class. Any object that interacts with the class can have access to the public members of the class. For example, public class StudentRecord { default access to instance variable public int name; default access to method public String getName{ return name; } } In this example, the instance variable name and the method getName can be accessed from other objects. Introduction to Programming I 167 J.E.D.I

10.9.3 protected access

This specifies that the class members are accessible only to methods in that class and the subclasses of the class. For example, public class StudentRecord { default access to instance variable protected int name; default access to method protected String getName{ return name; } } In this example, the instance variable name and the method getName can be accessed only from methods inside the class and from subclasses of StudentRecord. We will discuss about subclasses on the next chapter.

10.9.4 private access

This specifies that the class members are only accessible by the class they are defined in. For example, public class StudentRecord { default access to instance variable private int name; default access to method private String getName{ return name; } } In this example, the instance variable name and the method getName can be accessed only from methods inside the class. Coding Guidelines: The instance variables of a class should normally be declared private, and the class will just provide accessor and mutator methods to these variables. Introduction to Programming I 168 J.E.D.I

10.10 Exercises