Geeks With Blogs
Daniel Forhan Computer Science Teacher and Java Programmer

Each year that I have taught Java or AP Computer Science I have found my that students tend to get confused with the proper use of modifiers when designing classes. Partly from a misunderstanding of what the modifiers mean, and partly because they haven't really developed a strong conception what the whole OOP paradigm is all about. So here is a brief explanation that I give to my students, starting with a few definitions. This usually helps to clear this matter up a bit.


public- Can be accessed from any class from any location.
default -Available to classes in the same folder/package (no modifier)

protected- Available to all classes in the same package and its subclasses.

private- Same class access only. Not available outside the class defined in.

Now when using these modifiers we have to be careful. Some students think that having public methods means that your data is exposed or unprotected. I get questions like, “shouldn't methods be private just as the instance fields are private?“ Of course I tell them no. To help them understand why I remind them of some of the OOP principles that come into play here and how we model that with the proper use of modifiers. One of the basic principles in object oriented programming is the who idea of "data hiding" or "encapsulation". The way that works is you design your classes so that data can only be changed by the class that it is defined it. That is accomplished by declaring your instance variables (class variables) as "private". If they are private no class can change them other than the class that they are defined in.

The question remains, how can other classes use that data? Well it is simple, the class has a *public interface* . These are the class methods (ex. getSum() ). So we need to have public methods otherwise no other class could use our data. These methods update (mutator methods), and provide access (accessor methods) to or private data.

Classes that use our class need an object to access those methods, so it makes no difference if two or more classes have the same method names (although you should avoid this) because you need an object of the class to access them, and the object decides what class methods will be used. [And remember that classes are designed so that other classes can call their methods. If a class method is private then no other class can use it.]

Now that all said, there are some cases that we might declare a method as private. Occasionally your class might need what is called a helper method, which is a method that supports another method in the class. In other words other classes don't ever need to call it or use it, because it serves the class itself. So the bottom line is to design classes with a constructor that initalizes the private data (instance fields), and a public interface (mutator and accessor methods) that acts on the private data. And remember to make methods private only when their purpose is to serve the class that they are defined in.

Posted on Monday, October 31, 2005 5:45 AM | Back to top

Copyright © Daniel Forhan | Powered by: