Encapsulation - Access ModifiersS2C Home « Encapsulation - Access Modifiers
Up until now we haven't really worried about our data and how we expose it to the outside world. Before we do so, lets take a step back and explain what we mean by exposing our data.
Taking our Cat
class test methods
as an example; we have been happily writing code such as moggy.age = 12;
without a care in the world to make sure our Cat
class behaves as we expect. We could just have easily written moggy.age = 999;
or moggy.age = -234;
and the code would have worked just fine. Of course we could have verified the values we sent to our Cat
class before we sent them, but this would have cluttered the code. Even if
we had done this, there was nothing in place to stop any other program from making changes to our Cat
members. In the real world we can't expose data like this, and we learn not to, as we look at encapsulation.
So what can we do about protecting our data and what is encapsulation? Encapsulation is a language mechanism used to restrict access to an object's components. in Java parlance this is a mechanism to restrict access to the members (instance variables and methods) of a class, constructors and even to the class itself. So how do we achieve this in Java?.
Well Java provides us with the access modifier mechanism for restricting access to our classes, which are
the basic unit of encapsulation in Java. We can also restrict access to our instance variables whilst providing access to them via public
methods. We can limit construction to the class itself using the
private
keyword, or to the package the implementation is in using the protected
keyword or with no modifier package-private / (the default). We have already seen the public
access modifier
used throughout the lessons and now it's time to go into access modifiers in more detail.
Access Modifier Table Top
The table below shows the four types of access available in Java from the most open (public
) to the most restrictive (private
). We can only explicitly apply the public
access modifier
to our top-level classes (the classes we compile) but for members and constructors we can explicitly apply the protected
and private
access modifiers as well. We will
talk about packaging in the Packages lesson, but for now we are going to examine how to protect our class members from unwanted access and modification.
Access modifier | Class | Member | Cons |
Description |
---|---|---|---|---|
public | Yes | Yes | Yes | A top-level class may be declared with the public access modifier, and if it is the class is accessible to all other classes everywhere.A member may be declared with the public access modifier, and if it is the member is accessible to all other classes everywhere, assuming the class it resides in is accessible.A constructor may be declared with the public access modifier, and if it is the constructor is accessible to all other classes everywhere, assuming the class it resides in is accessible. |
protec | No | Yes | Yes | A member may be declared with the protected access modifier, and if so, is only accessible within its own package and also by a subclass of its class in other packages.A constructor may be declared with the protected access modifier, and if so, it is only accessible to the package
the implementation is in.See the Packages lesson for more information on packaging. See the Inheritance Basics lesson for more information on subclassing. |
no modifier package-private / (the default) | Yes | Yes | Yes | If a top-level class has no explicit access modifier, which is the default and is also known as package-private,
it is accessible only within its own package. If a member has no explicit access modifier it is only accessible within its own package. If a constructor has no explicit access modifier, it is only accessible to the package the implementation is in. See the Packages lesson for more information on packaging. |
private | No | Yes | Yes | A member may be declared with the private access modifier, and if it is the member is only accessible within its own class.A constructor may be declared with the private access modifier, and if it is the constructor can only be constructed from within its own class. |
Related Quiz
OO Concepts Quiz 1 - Encapsulation - Access Modifiers
Lesson 1 Complete
In this lesson we took a first look at encapsulation and how we control exposure of our data to the outside world through the use of access modifiers.
What's Next?
In the next lesson we look at using access modifiers with examples on how to protect our data.