Java

Modifier in java With Programming Examples

Description:

Modifier in java: in this article, I am going to show you how to use different access modifiers in java. The main modifiers are public, private, protected, static, final,  abstract, transient, volatile, synchronized, and native.


Modifier in java:

Java provides modifiers for many areas. These can be used to mark classes, attributes, and/or methods for various purposes.

The modifiers are: public, private, protected, static, final, abstract, transient, volatile, synchronized, and native. The most well-known modifiers are probably the public, private, and protected access modifiers, which are used to control the visibility of the class, attribute, or method.

The following overview shows you which areas each modifier is used for:

Attribute Method Constructor Class Interface
abstract X X x
final X X X
native X
private X X X
protected X X X
public X X X x x
static X X
synchronized X
transient X
volatile X



Abstract access modifier in java

The abstract access modifier in java can be used on a method, class, or interface. This means that the element marked with abstract has not yet been fully implemented. If a method or an interface has the modified abstract, the implementing class must also be declared as abstract. If a class is marked with abstract, no object can be created from it.

A subclass derived from an abstract class must fully implement all abstract methods of the parent class, otherwise, it must also be declared an abstract modifier. In contrast to an interface, an abstract class can also contain implemented methods and attributes (that are not declared as static or final ). If there is no implementation at all for an abstract class, it makes more sense to declare it as an interface.

Example: how to use abstract modifier in java:

output:

Modifier in java

The class Show contains the abstract method Message. This method is marked with abstract and therefore has no method body. The method header ends directly with a semicolon. Because the Message method is abstract, the Show class must also be abstract.


final access modifier in java:

The final access modifier in java can be used on attributes, methods, and classes. Attributes with final are constants and can no longer be changed. Methods that have the modifier final cannot be overridden when inherited. A class, on the other hand, is at the end of an inheritance hierarchy if it is declared as final. No further subclasses can be derived from it.

Example:

// Constant PI can no longer be changed

final  double PI =  3.14 ;

The example above shows the use of final. In this case, it is a constant that can no longer be changed.

Example: how to use final modifier in java:

Output:
You will get compile-time error

Modifier in java


Native modifier:

Only methods can be marked with the modifier native. These methods are written in another programming language and are used for “multilingual” communication. Since native is more likely to be used in the more professional area, we will not give an example at this point.

Private access modifier in java

The private access modifier in java can be applied to attributes, methods, and constructors.  These cannot then be seen and used by another class (not even by derived classes). For better data encapsulation, attributes should be provided with this modifier as far as possible.


Example: how to use private access modifier in java:

output:

You will get the following error while compiling the code because we cannot access the private variable data in the Test class

Modifier in java



Protected access modifier in java

The protected access modifier in java can be used in the same way as private, but here the visibility is limited to the package and not just to your own class, as with private. Subclasses could also always “see” the members of the methods declared protected.

 Example: how to use protected access modifier in java:

output:

Modifier in java


Public access modifier in java

The public access modifier is allowed for all constructs. The elements declared in this way can be used by any class, so they are public. Attributes of your own class that are marked private can also be read out and changed via public methods.

Example: how to use public access modifier in java:

output:

Modifier in java


Static modifier:

Class variables or class methods are marked with the modifier static.

Class methods are independent of objects. The static method is therefore called via the class name and not via the object name or this pointer.

Unlike attributes, class variables have the same value for every object in the class. In a sense, the instances of the class share the variable. If the class variable is changed in one object, all other objects have also received the change.

Example: how to use static keyword modifier in java:

output:

Modifier in java


Synchronized modifier:

Only methods are marked with the synchronized modifier in java. This ensures data consistency when using threads if one thread wants to read and another thread wants to write to data.

Example: how to use Synchronized modifier in java:

output:

Modifier in java



Transient modifier:

Attributes marked with the modifier transient are not taken into account during serialization, which means, for example, that these attributes are not written to a file.

output:

Modifier in java


volatile modifier:

Attributes marked with the modifier volatile allow concurrent access by two or more threads.

Example: how to use volatile modifier in java:

output:

When the ready is true you will see the following output.

Modifier in java

And when  you change the ready from  true to false you will see the following output:

output:

Modifier in java

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button