Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

Member function visibility in Java programs

Making it effective

Scott W. Ambler, Practice Leader, Agile Development, Rational Methods Group, IBM, Software Group
Scott W. Ambler is President of Ronin International, a consulting firm specializing in object-oriented software process mentoring, architectural modeling, and Enterprise JavaBeans (EJB) development. He has authored or co-authored several books about object-oriented development, including the recently released The Object Primer 2nd Edition, which covers, in detail, the subjects summarized in this article. He can be reached at scott.ambler@ronin-intl.com and at his Web site at www.ambysoft.com.

Summary:  The visibility of a Java member function defines a Java object's level of access to it. My experience has been that the choice of visibility is an important design decision as well as an important implementation decision because it is one way to reduce the coupling within your system. This week's topic is modified from Chapters 7 and 8 of The Object Primer 2nd Edition.

Date:  22 Sep 2000
Level:  Introductory

Comments:  

From a design point of view, it is important to note that the Unified Modeling Language (UML) offers three levels of visibility: public, protected, and private, as indicated in Table 1. For programmers, it is good to know that the Java programming language supports these three visibilities and adds a fourth: default. Table 1 describes each type of visibility supported by the Java programming language, indicating the appropriate UML symbol, the Java keyword that you would apply when declaring a member function, a description of what each visibility means, and my advice for applying each type of visibility effectively. Listing 1 depicts how to declare a member function in the UML; notice how the visibility is indicated with the "+" symbol. Listing 2 presents the declaration of that same member function in Java code; notice the use of the keyword public in its signature.

My general rule of thumb is to be as restrictive as possible when setting the visibility of a method. In other words, if a method doesn't have to be public, then make it protected. If it doesn't have to be protected, then make it private. Finally, use default visibility only when you mean it and document your reasoning for applying that visibility in the method header documentation if it isn't clear why you applied it. You may need to refer to the tip Documenting Java member functions, in which we discussed member function documentation.

Table 1. Visibility of Java member functions
VisibilityUML symbolJava keywordDescriptionSuggested usage
Public+publicA public member function can be invoked by any other member function in any other object or class.When the member function must be accessible by objects and classes outside of the class hierarchy in which the member function is defined.
Protected#protectedA protected member function can be invoked by any member function in the class in which it is defined or any subclasses of that class. When the member function provides behavior that is needed internally within the class hierarchy but not externally.
Private-privateA private member function can only be invoked by other member functions in the class in which it is defined, but not in the subclasses.When the member function provides behavior that is specific to the class. Private member functions are often the result of refactoring, also known as reorganizing, the behavior of other member functions within the class to encapsulate one specific behavior.
DefaultNo symbol availableNo keyword, simply leave it blankThe member function is effectively public to all other classes within the same package, but private to classes external to the package. This is sometimes called package visibility or friendly visibility.This is an interesting feature, but be careful with its use. I use it when I'm building domain components, collections of classes that implement a cohesive business concept such as "Customer", to restrict access to only the classes within the component/package.



Listing 1. Declaration of the signature of a member function in the UML

+ hasParkingPrivileges(): boolean




Listing 2. Declaration of a simple member function in Java code
	
public boolean hasParkingPrivileges()
{
     return true;
}


Resources

About the author

Scott W. Ambler is President of Ronin International, a consulting firm specializing in object-oriented software process mentoring, architectural modeling, and Enterprise JavaBeans (EJB) development. He has authored or co-authored several books about object-oriented development, including the recently released The Object Primer 2nd Edition, which covers, in detail, the subjects summarized in this article. He can be reached at scott.ambler@ronin-intl.com and at his Web site at www.ambysoft.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and web services
ArticleID=10468
ArticleTitle=Member function visibility in Java programs
publish-date=09222000
author1-email=scott_ambler@ca.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).