You are on page 1of 3

Visibility Modifiers

Lawrence M. Brown ktee@erols.com

Visibility Modifiers
Access to class instance variables and class methods are controlled through access modifiers. These modifiers designate who can see the data and methods inside the object. A. public This indicates that the variable or method can be accessed by anyone who can access an instance of the class. A class may also be designated public, which means that any other class can use the class definition. The name of a public class must match the filename, thus a file can have only one public class.

B. private A private variable or method is only accessible from methods within the same class.

Declaring a class variable private "hides" the data within the class, making the data available outside the class only through method calls. This technique is known an encapsulation, because the data is sealed safely inside a "capsule" of the class. The data can only be accessed through trusted methods. C. protected Protected variables or methods can only be accessed by methods within the class, within classes in the same package, and within subclasses. Protected variables or methods are inherited by subclasses of the same or different package.

D. [default] A variable or method has default visibility if a modifier is omitted. Default visibility indicates that the variable or method can be accessed by methods within the class, within classes in the same package, but is not inherited by subclasses.

Use public access for variables and methods that should be visible everywhere the class is visible. Use private access for variables and methods that are only used within the class itself and that should be hidden from everywhere else. Use no access modifier (default visibility) for variables and methods that should be hidden from other packages, but available to other classes and subclasses within the same package. Note: The default visibility is more restrictive than protected visibility. Protected variables and methods may be inherited by subclasses in a different package, whereas variables and methods with default visibility may not.
1
December 31, 1998

1998

Visibility Modifiers

Lawrence M. Brown ktee@erols.com

Modifiers
Data Fields and Methods Accessible from same class? Accessible to classes (nonsubclass) from the same package? Accessible to subclass from the same package? Accessible to classes (nonsubclass) from different package? Accessible to subclasses from different package? Inherited by subclass in the same package? Inherited by subclass in different package? public yes yes protected yes yes default yes yes private yes no

yes

yes

yes

no

yes

no

no

no

yes

no

no

no

yes

yes

yes

no

yes

yes

no

no

Adapted from: David Flanagan, Java in a Nutshell, OReilly & Associates, Inc., May 1996.

Example, Using Public and Private Access File Circle.java:


package shapes; class Circle { public double x, y, radius; private double MAXR = 10.0; private boolean check_radius( double r ) { return (r < MAXR); } // Circle constructor // initializes each instance variable to zero public Circle() { x = y = radius = 0.0; } Class Circle has default visibility, can only be seen within the same package. Can be seen by class Square (same package).

x, y, and radius are public and available anywhere the class can be instantiated. The class circle has default visibility, though public, x, y, and radius are only accessible within the package through an instance of a Circle object.

// Circle Constructor // x, y position supplied, along with the radius public Circle( double x, double y, double radius ) { this.x = x; this.y = y; this.radius = check_radius( radius ) ? radius : } } // end class Circle MAXR;

1998

December 31, 1998

Visibility Modifiers

Lawrence M. Brown ktee@erols.com

File Square.java:
package shapes; class Square { public double w, h; private double MAXW = 10.0; private double MAXH = 25.0; private boolean check_width( double w ) { return (w < MAXW); } private boolean check_height( double h ) { return (h < MAXH); } // Square constructor // initializes each instance variable to zero public Square() { w = h = 0.0; } // Square Constructor // w, h position supplied public Square( double w, double h ) { this.w = check_width(w) ? w : MAXW; this.h = check_height( h ) ? h : MAXH; } } // end class Square Default visibility, can only be seen within the same package. Can be seen by class Circle (same package). Use of "import shapes" still does not permit access to class Square (default visibility). Private instance variables only accessible within the class.

Private methods only accessible within the class.

1998

December 31, 1998

You might also like