Access Modifiers In Java – Keeping Your Privates Private

Access modifiers in Java helps us choose what to share with the world and what to keep to ourselves; you know, like your secrets. Perhaps you use them everyday; you know everything you need. Perhaps you are still unsure what they really mean. It does not hurt to refresh your mind because you really can’t know enough. That is why am going to lay everything about access modifiers in java on the table. Dissect them with examples and make it known to all in a simple way. Let us get started.

access modifiers in java

Let us debunk access modifiers in java and take away the confusion that surrounds them.

Access Modifiers In Java Uncovered

There are four access controls (access levels) in Java. There are however 3 access modifiers. The fourth access control – also known as default or package access, is what you get whenever you don’t use any of the three access modifiers – public, private & protected.

For this article, am going to focus on methods & variable members because their access controls are given in the same way.

The one thing that can be a little confusing is the difference between default access modifier and protected access modifier in Java – will talk about this later in this post. The need to fully understand these concepts might not be apparent to you but being able to use them correctly (this is all about code design) is important.

If a class A has access to a member of class B’s member is visible to class A. If you attempt to access a member of another class without access rights, the compiler will yell at you for doing that. As far as access goes, you need to understand two different access issues;

  • Whether method code in one class can access a member of another class.
  • Whether a subclass can inherit a member of its superclass.

The first type of access is when a method in one class tries to access a method or a variable in another class, using the dot operator (.) to invoke a method or retrieve a variable.

The second type of access revolves around which, if any, members of a superclass a subclass can access through inheritance. This is not looking at whether a subclass can invoke a method on an instance of the superclass – that is what the first type implies. We are instead looking at whether the subclass inherits a member of its superclass.

Sample Code – Inheritance

Much of the access controls (both types) centers on whether the two involved classes are in the same or different packages. Remember however that if class A cannot be accessed by class B, then no members of class A are accessible to class B. Now, let us narrow down to the commonly used access modifiers: public, private, default and protected.

Public Access Modifier

When a method or variable member is declared public, it means all other classes, regardless of the package they belong to, can access the member – assuming of course the class itself is visible to the public. Here is an example:

In the above code, it is obvious that the two classes (Goo and Sludge) are in two different packages but Goo can still invoke the method in Sludge without problems. This is possible because both Sludge and its method testIt() are marked as public.

For a subclass, if a member of its superclass is declared public, the subclass inherits that member regardless of whether both classes are in the same package. Here is an example:

The Roo class declares the doRooThings() method as public. So, if we make a subclass of Cloo, any code in that Cloo subclass can call its own inherited doRooThings() method. Here is an example:

Notice that the doRooThings() method in the preceding code was invoked without having to use a reference at all. It not only means the method belongs to the invoking class but also implicitly being accessed using the this reference.

You can also, besides invoking doRooThings() method on its own, from some other class, use a Cloo instance to invoke the method simply because it is public. Here is an example;

Private Members

Members marked private cannot be accessed by code in any class other than the class in which the private member was declared. Let us make a slight change to our previous Roo class;

The doRooThings() method is now private. That implies that no other class can use it. An attempt to invoke it from another class will result in a compiler trouble. Here is an example;

Can a subclass inherit a private member of its superclass? The answer is NO. To make it sound even dirtier, a subclass cannot see, touch or even think about the private members of its superclass.

NOTE: Although you are allowed to mark instance variables as public, in practice, it is nearly always best to keep all variables private or protected. You can then use public getter and setter methods to get and set their values.

You might also ask a question like this : Can a private method be overridden by a subclass? The answer is technically no. Since a subclass cannot inherit a private method, there is no way it can override it. Overriding depends on inheritance.

Protected and Default Members

The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package. Whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package. Here are some examples;

After looking closely at the above code, do you think it will actually run? Obviously it won’t work.

That is because first of all, testIt() method has default access modifier. Secondly, the two classes are in two different packages.

Default and protected behavior differ only when we talk about subclasses. If the protected keyword is used to define a member, any subclass of the class declaring the member can access it through inheritance. It does not matter if the superclass and the subclass are in two different packages.

You can read more about this topic by visiting the documentation here.

If you have any questions, suggestions or comments, please do not hesitate to let me know. If you found this article helpful, please consider sharing it using the buttons below. Thanks again and please stay tuned for more posts.

Stay informed!

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

One Comment on “Access Modifiers In Java – Keeping Your Privates Private

  1. Mark Lewis Reply

    July 28, 2014 at 6:55

    Access modifiers can be tricky to understand. This and this articles clarifies it.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.