Copyright

Java Keywords: Protected, Public & Private

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java is great at protecting objects. What does it mean to protect an object? Protected, public and private are keywords used to control access. This lesson will define the concepts and provide real-world examples.

Protecting Objects and Classes

Object-oriented programming, and Java in particular, provides wonderful tools for controlling access to objects and classes, and the variables and methods within those classes.

In this lesson, we'll cover the concepts and the code presented will revolve around a hypothetical program that works with employee information.

Public, protected, and private are the keywords used when you declare a variable or a method. Each one tells Java how much to share about that variable or method. A public method is wide-open to other parts of the program. And as you'd expect, private is more restrictive.

Let's cover each one of these keywords in a little more detail, working our way from least restrictive to most restrictive.

Public

This is the most open declaration. Public classes and their methods are freely available. In our employee example, we will create a public class that has some employee information. This information is considered in the public view - name, job code and so on. Pay rate, home address and others will not be public, but we'll cover those later.


public class Employee_Public_View {
 public String employeeName = new String();
 public int jobCode;
}


Protected

If you want to protect members of a class, but still allow other classes in the package, or subclasses to use them, the protected keyword can be used. A subclass, or child class, is a class that inherits variables and methods from another class. Private members can't be shared down to children, but protected members can.

A package in Java is like a project: it is a group of similar class types all contained within the same set of code. It is easier to work with separate packages, say Employee, and have all Employee-related classes and methods within the package.

The reason for the protected keyword is to ensure the variables don't get used outside of the package. If you created a Union package and wanted it to be able to access some things from Employee, make sure to create those as public. Items that are protected or private can't be used outside of our Employee package.

In the following example we create two classes, one for union and one for the bargaining unit (which is a subclass of the first). Pay attention to the comments in the code - the protected variables and methods can't be used outside of the current package.


package Employee;
public class Employee_Union {
 public int ID;
 //you can use this in other packages
 protected int unionCode;
 protected void getUnion() {
  //method code here; both method and variable CANNOT be used in other packages
 }
}
class Employee_Bargain_Unit extends Employee_Union{
 void getBargainUnit() {
  Employee_Union union = new Employee_Union();
 }
}


Now let's say we create a whole new package for some other work we will be doing. We'll import the original Employee package and create a new class that extends the Employee_Union class. This will be allowed, except we can't use any of the protected variables or methods from the parent class.


import Employee.*;
package newUnion;
class New_Union extends Employee_Union {
 //can't use any protected variables from other package
}


Protected has its uses, but the most commonly used options are public and private. Either we want to have our classes and methods available globally or we want to protect them entirely.

Let's look at the private keyword next.

Private

The private keyword provides the highest level of protection for members of classes.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support