Java Class Modifiers
Introduction
Java is a powerful, general-purpose programming language. It is an object-oriented language which allows developers to create reusable components or objects that can be used in other programs. It is widely used in the software development industry and is renowned for its reliability, flexibility, and security. One of the most important aspects of the Java language is its use of class modifiers. These modifiers allow developers to control the access of classes, variables, and methods and the way they are used in the program. Using class modifiers, developers can control visibility, create abstract classes, and use static members.
In this article, we'll discuss the Java class modifiers in detail. We'll discuss their purpose, syntax, and usage. We'll also look at examples to illustrate how to use class modifiers in your programs.
What Are Class Modifiers?
Class modifiers can be applied to a Java program's classes, variables, and methods. They are used to control the visibility and usage of these entities.
Class modifiers can be divided into two categories: access modifiers and non-access modifiers. Access modifiers control the accessibility of classes, variables, and methods. Non-access modifiers control the usage of classes, variables, and methods.
Access Modifiers
Access modifiers control a program's classes, variables, and methods visibility. They determine which parts of the program can access the entities.
Java has four access modifiers: public, protected, private, and default (or package-private).
Public
The most lenient modification is public access. It enables any class in the application to access the object. It is the sole access modifier that top-level classes can use.
Protected
Compared to public access, the protected access modifier is more restrictive. It permits access to the object from any class in the same package as well as from subclasses of the class.
Private
The least tolerant modification is private access. Only the class in which the entity is declared is able to access it.
Default
The default access modifier, also known as package-private, is less permissive than protected. It allows the entity to be accessed only by classes in the same package.
Non-Access Modifiers
Non-access modifiers control the usage of classes, variables, and methods. They determine how the entities can be used in the program.
There are four non-access modifiers in Java: final, abstract, static, and synchronised.
Final
The final modifier indicates that a class, variable, or method is not meant to be changed or extended. It creates constants, prevents method overriding, and prevents class inheritance.
Abstract
The abstract modifier indicates that a class is not meant to be instantiated. Abstract classes are meant to be extended, not instantiated. They are used to create base classes that define common behaviour for subclasses.
Static
The static modifier indicates that a class, variable, or method is meant to be shared among all class instances. It is used to create class methods, variables, and constants.
Synchronised
The synchronised modifier denotes that only one thread shall run a method at once. It is applied to guarantee thread safety for the procedure.
Uses of Class Modifiers
Now that we've discussed the different class modifiers let's look at how to use them in our programs.
Access Modifiers
Access modifiers control the visibility of classes, variables, and methods. To use an access modifier, add it before the entity's declaration. For example, to make a class public, you would use the following syntax:
public class MyClass {
// class code goes here
}
Non-Access Modifiers
Non-access modifiers control the usage of classes, variables, and methods. To use a non-access modifier, add it before the entity's declaration. For example, to make a class final, you would use the following syntax:
final class MyClass {
// class code goes here
}
Program
public class Modifiers {
// The public modifier allows a class, method, or field to be accessed by any class, anywhere in the program.
public int publicNumber = 1;
protected int protectedNumber = 2;
// The default modifier (also known as package-private) allows a class, method, or field to be accessed only by classes in the same package.
int defaultNumber = 3;
// The private modifier allows a class, method, or field to be accessed only from within the same class in which it is declared.
private int privateNumber = 4;
public static void main(String[] args) {
Modifiers modifiers = new Modifiers();
System.out.println("publicNumber: " + modifiers.publicNumber);
System.out.println("protectedNumber: " + modifiers.protectedNumber);
System.out.println("defaultNumber: " + modifiers.defaultNumber);
System.out.println("privateNumber: " + modifiers.privateNumber);
}
}
Output
publicNumber: 1
protectedNumber: 2
defaultNumber: 3
privateNumber: 4
Conclusion
In this article, we discussed the different class modifiers in Java. We discussed their purpose, syntax, and usage. We also looked at examples of how to use class modifiers in your programs.
Class modifiers are an important part of the Java language. Using them allows you to control the visibility and usage of classes, variables, and methods. This allows you to create reliable, secure, and flexible programs.