Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.9. Access modifiers

4.9. Access modifiers
Last updated: 23 January 2013.

Access modifiers allow you to manage the visibility of classes, fields, methods and constructors. Java has 4 access modifiers:

public
private
protected
default


4.9.1. The public access modifier

So far, most of the classes, fields, methods and constructors that I have used were declared public. The keyword public is an access modifier which says that the class, field, method or constructor it is applied to is visible from any other class, even from the classes belonging to other packages.


4.9.2. The private access modifier

A field, method, constructor or nested class that is declared private is only visible within the enclosing class. The private access modifier is the most restrictive one. It cannot be applied to interfaces and classes that are not nested classes. Identically, constants and methods that are declared in an interface cannot be declared private. It is considered as a good practice to declare private all the fields of a class and write getters and setters. The methods that are called only in the enclosing class should also be declared private. As an example, the class Aircraft shown below illustrates the use of private fields as well as getters and setters:

public class Aircraft {

   
private float length;
   
private float height;
   
private int range;
   
private double weight;
   
private boolean airborne;


   
public Aircraft(){
         
System.out.println("Aircraft created");
   
}


   
/*
     * Getters and setters
     */

   
public float getLength() {
         
return length;
   
}

   
public void setLength(float length) {
         
this.length = length;
   
}

   
public float getHeight() {
         
return height;
   
}

   
public void setHeight(float height) {
         
this.height = height;
   
}

   
public int getRange() {
         
return range;
   
}

   
public void setRange(int range) {
         
this.range = range;
   
}

   
public double getWeight() {
         
return weight;
   
}

   
public void setWeight(double weight) {
         
this.weight = weight;
   
}

   
public boolean isAirborne() {
         
return airborne;
   
}

   
public void setAirborne(boolean airborne) {
         
this.airborne = airborne;
   
}

}

If you try to compile the class App shown below, you will get a compilation error:

public final class App {

   
public static void main(String[] args) {
         
Aircraft aircraft = new Aircraft();
          System.out.println
(aircraft.range);
   
}

}

The reason is that the field range is not visible from outside the class Aircraft and, as a result, it is impossible to access it directly via its name. Instead, you must use the getter and the setter to access the field range:

aircraft.setRange(2500);
int range = aircraft.getRange();


4.9.3. The protected access modifier

A field, method, constructor or nested class that is declared protected can only be accessed by classes of the same package and by subclasses of other packages. The protected access modifier cannot be applied to interfaces. It also cannot be applied to classes that are not nested classes. Identically, constants and methods that are declared in an interface cannot be declared protected.


4.9.4. The default access modifier

When no access modifier is specified, the default access modifier is applied. A field, method, constructor or class that does not have any access modifier can only be accessed by classes of the same package. The default access modifier cannot be applied to constants and methods that are declared in an interface.


4.9.5. About interfaces

When no access modifier is specified, the default access modifier is not applied to the constants and methods of interfaces. Instead, the constants and methods of interfaces are implicitly public. Moreover, the constants are implicitly static and final whereas the methods are implicitly abstract as you have seen in the interfaces tutorial. Hence, the following interface definitions are the same:

public interface AircraftInterface {

   
int MINIMUM_TAKEOFF_DISTANCE = 0;
   
int MAXIMUM_CREW_COUNT = 30;

   
boolean land();
   
boolean takeOff();

}


public interface AircraftInterface {

   
public static final int MINIMUM_TAKEOFF_DISTANCE = 0;
   
public static final int MAXIMUM_CREW_COUNT = 30;

   
public abstract boolean land();
   
public abstract boolean takeOff();

}


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.9. Access modifiers
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.10. The Java API

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7