5.1 Anatomy of a Class
In Unit 2, you had the opportunity to explore classes from the perspective of a user of the class. In this unit you will take a deeper dive and look at how you can create classes of your own. Classes allow you to create custom objects that can make your program easier to implement.
In Java, there are two main access levels that you will see in this course, private
and public
. Private access is used when an element (such as a variable or method) should only be able to be accessed within the declaring class. What does that mean? Access in the class is when other statements in the same class try to access a value or function. For example given the class definition below:
Any statement in the Sample
class will be able to access value
, but any other class that may create a Sample
object will not have access to value
since it is private.
In contrast, public access allows for an element to be accessed by any other class. Take a look at the updated class definition below:
In this example, you will notice that the getValue
method is public. In this case, if a Sample
object was created in another class, you would be able to call the getValue
method, and thus get indirect access to value
.
When creating classes, classes should always be declared public since they need to be accessed by other classes. Likewise, a constructor in the class should always be public since it needs to be called by external classes.
When designing a class, programmers can make certain decisions about which elements should be public or private, however there are some basic governing rules that should be followed.
When it comes to instance variables, all instance variables should be designated private to keep access internal to the class. There are several reasons why you should always declare instance variables private.
The first reason revolves around the idea of data encapsulation. Data encapsulation is a technique in which the implementation of details of the class are kept hidden from the user. By using the private level access, instance variables are encapsulated in the class.
When designing a class, programmers make decisions about what data to make accessible and modifiable from an external class. Each piece of data can be either accessible or modifiable, or it can be both or neither. This access is accomplished through accessor and mutator methods (getters and setters).
In addition to data encapsulation, private access to instance variables can be used to help manage complexity. For example, think about a student object that may have an instance variable for grade and another variable to designate school level (such as middle school or high school). A programmer may decide to limit modifications to only the grade level and within the grade mutator method they also make updates to the school level. Not only does this streamline the update process, but it also controls the process to make sure you don’t have a 12th grade middle school student.
While instance variables are always declared private, methods can be declared either public or private, making them either external or internal to the class. Many behaviors need to be available externally, so oftentimes methods are declared public, but there are times when private access is preferable.
Private access is usually used with helper methods. For example, think back to the student object described above. The programmer may decide to write a public mutator method to change the grade. Once the grade is changed, the school level needs to be checked and possibly changed. If the programmer decided to do this check and make the change using another method, then that method would be private. As a private method, it can be called from the grade mutator method, but the end user cannot call the method directly from the other class.
Programmers will use private methods when they want to have a method to use in their class, but do not want that method called and used from an instance of the object.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Which of the following class members should NOT be public?
In this exercise, you are going to create the instance variables for an Employee class. The class needs to store the employee’s name, number of years of service, number of vacation days available (can be fractional days). You will need to give the instance variable an appropriate name, type, and privacy settings.
After defining the instance variables, create the structure for the constructor Make sure you set the privacy settings on the constructor correctly.
Note: Because there is no main method, your code will not execute (that’s ok). Use the Test Cases to verify that you have the correct code.