Java, Java Tutorial, Software Development

Chapter 7: Java Classes And Access Modifiers – Review

Java classes and their variables and methods can use access modifiers – private, public, protected, and package-level (no keyword). These access modifiers set the classes’ visibility, constructors, and instance or class variables and methods to the other parts of the application codes.

Top-Level Java Classes

The TopLevelClass1 class is a top-level class. It is the only public class in a .java file that has the same name – TopLevelClass1.java.

Let’s say we added another public class in the same file. Now, the codes will not compile.

The IDE or the javac command gives out the following compile-time error.

Similarly, if we had other classes that are private and protected, the codes will fail compilation.

For the private class, the compiler generates the following error.

For the protected class, the compiler generates the following error.

Java Classes And Access Modifiers

However, if we only had package-level classes (those with no access modifier before the class keyword), everything works – no compilation errors.

In summary, a top-level class is a Java class that is not within any other block of codes. Moreover, it has the same name as the .java file where it is stored. Also, it can only be either a public or a package-level class. The rules also apply to Java interfaces – on later posts.

From here on, we interchangeably refer to a Java class’ instance and class variables as properties and the methods as behaviors.

Default Constructors Access Modifiers

Default constructors are constructors that the compiler inserts into our class codes during compilation when we don’t create our constructors. A default constructor’s default access modifier is the class’ access modifier.

Consider the following package-level class.

For a class with a package-level access modifier, its default constructor will have the same access modifier.

The Four Access Modifiers in Java

There are four access modifiers that we can use on classes, both instance, and static variables and methods. They set the visibility of the class itself and its variables and methods to the other parts of the program. If they aren’t visible, the other codes won’t use them, and the compilation will fail. In Java, there are four access modifiers.

The private Access Modifier

The private access modifier makes an instance or class variables and methods visible only to the codes within the class. Consider the following Java class with both a private property and a private method that are not visible to other codes.

Now, we have the StartupClass class that uses the TopLevelClassForPrivateAccessModifier class’s property and method.

When we run these codes, we will get compilation errors.

The TopLevelClassForPrivateAccessModifier class’ property and method are not visible to the StartupClass class. On the other hand, if we moved the main method to the TopLevelClassForPrivateAccessModifier class and run it, the codes compile, and the application runs successfully.

Java Classes And Access Modifiers

The public Access Modifier

The public access modifier makes the class itself, its instance, or class variables and methods visible to all other codes. Consider the following new class.

Next, we modify our StartupClass class to use the new Java class.

The codes compile and run successfully.

Java Classes And Access Modifiers

The no-keyword Package-Level Access Modifier

The package-level access modifier does not have a keyword compared to the other access modifiers. The class’ constructors, properties, and methods are visible to codes in the same package with the package-level access modifier. Consider the following class in package com.turreta.accessmodifiers.package1.

Our modified StartupClass class is a different from that of TopLevelClassForProtectedAccessModifier.

When we compile these codes, we get the following errors.

Suppose we moved StartupClass to the com.turreta.accessmodifiers.package1 package; the codes will compile and run successfully.

Java Classes And Access Modifiers

The protected Access Modifier

The protected access modifier works like the package-level access modifier. In addition to that, it works with inheritance and bypasses the package-level access restriction. Consider the following classes.

The  ProtectedAccessModifierClass class has a protected property and a protected method. Then, we create another class that extends ProtectedAccessModifierClass.

Although  ChildProtectedAccessModifierClass is in a different package from ProtectedAccessModifierClass, the child class has access to the parent class’s property and method via inheritance.

If we had another class that accesses the ProtectedAccessModifierClass class’s property and method, that class would fail compilation.

Java Classes And Access Modifiers

The Case Of Inner Classes In Java

We can nest Java classes. If we top-level classes, we also have inner classes that we define within other classes. These classes can be either instance or static classes.

Java Classes And Access Modifiers

 

What’s Next?

We have tackled Java Classes And Access Modifiers, but we only have scratched the surface of Java. Before we go any further, we ought to know what Java primitives are. They are data types that we cannot create objects of because they are non-OOP.

Chapter 8: Java Primitives in The OOP World

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like