java

Access Modifiers in Java

Modifiers are keywords that are added to variables, methods and classes to change their meaning.

There are two (2) types of modifiers:

  1. Access modifiers
  2. Non-access modifiers

Access Modifiers

There are four (4) access levels in Java, which are:

  1. Default. This is by default visible to the package and no modifiers are needed here.
  2. public (visible to the world)
  3. private (visible to the class only)
  4. protected (visible to the package and subclass)

Default Access Modifier

Default access modifier means that we do not explicitly declare any access modifier for a class, field, method, etc. This makes it accessible to any other class in the same package.

Private Access Modifier

Methods, variables and constructors that are declared private can only be accessed within the declared class itself. Private access modifier is the most restrictive access level. But you should note that class and interface cannot be private.

Protected Access Modifier

Variables, methods and constructors which are declared as protected in a superclass can be accessed only by the subclass in other package or any class within the package of the protected members’ class.

Public Access Modifier

This makes the method accessible to the whole world.

Non-access Modifiers

  1. Static Modifiers

(i) Static variables:

The static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists, regardless of the number of instances of the class.

Static variables are also known as class variables. Local variables cannot be declared static.

(ii) Static methods

This creates methods that will exist independently of any instances created fo the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take no reference to variables.

2. Final Modifiers

(i) Final variables

A final variable can only be initialized once. The final modifier is often used with the static keyword to create a constant and thereby make it a class variable. A final variable cannot be assigned another value.

(ii) Final class

A final class cannot be extended or subclassed.

The below example will not compile:

final class Animal {
//statements here ...
}

class Dog extends Animal {
//this will not throw an error
}

(iii) Final methods

When we use the final specifier with a method, the method cannot be overridden in any of the inheriting classes.

It is also necessary to point out that since private methods are inaccessible, they are implicitly final in Java.

3. Abstract Modifiers

(i) Abstract class:

Abstract classes can never be instantiated.

(ii) Abstract methods:

Abstract methods are methods declared are methods that are declared without any implementation and usually ends with a semi-colon.

Recent Posts

Google Announces that AI-developed Drug will be in Trials by the End of the Year

Isomorphic Labs, a drug discovery start-up launched four years ago and owned by Google’s parent…

4 hours ago

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

2 days ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

6 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

1 week ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

1 week ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago