top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Access Modifiers in Java

Introduction:

Java access modifiers are fundamental to object oriented programing, it provides various levels of encapsulation by controlling access to classes, fields, methods and constructors. Understanding these is crucial for designing secure, well-structured and maintainable java applications. Here's more detailed dive into each of the java access modifiers.


Types of Access Modifiers in Java:

There are four types of access modifiers available in java

  • Default (No keyword required)

  • Private

  • Protected

  • Public


Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.


Example:

In this example , we have created two packages exampledefault1 and exampledefault2 , so we are accessing the A class from outside its package , since A class is not public , so, it cannot be accessed from outside the package.


package exampledefault1;  
class A{  
  void msg(){
System.out.println("Hello");
}  
}  

//Another package
package exampledefault2;  
import pack.*;  
class B{  
  public static void main(String args[]){  
   A obj = new A();//Compile Time Error  
   obj.msg();//Compile Time Error  
  }  
}  

In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside the package.


Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class. The private access modifiers is specified using the keyword private .

Any other class of the same package will not be accessible these members, top level class ed or interfaces can not be declared as private. private means only visible with in the enclosing class.


Example:

package p1;

// Class A
class A {
    private void display()
    {
        System.out.println("Java");
    }
}

// Class B
class B {
    public static void main(String args[])
    {
        A obj = new A();
        // Trying to access private method
        // of another class
        obj.display();
    }
}

Output:
Java

Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.


Example:

class Animal {
    // protected method
    protected void display() {
        System.out.println("I am an animal");
    }
}

class Dog extends Animal {
    public static void main(String[] args) {

        // create an object of Dog class
        Dog dog = new Dog();
         // access protected method
        dog.display();
    }
}

Output:
I am an animal

In the above example we have a protected method named display() inside the animal class. The Animal class is inherited by the Dog class, and then we created an object dog of the Dog class, while using the object we tried to access the protected method of the class.



Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.


Example:

public class Animal {
    // public variable
    public int legCount;

    // public method
    public void display() {
        System.out.println("I am an animal.");
        System.out.println("I have " + legCount + " legs.");
    }
}

// Main.java
public class Main {
    public static void main( String[] args ) {
        // accessing the public class
        Animal animal = new Animal();

        // accessing the public variable
        animal.legCount = 4;
        // accessing the public method
        animal.display();
    }
}

Output:
I am an animal.
I have 4 legs.

In the above example Animal is accessed from the main class, variable legcount is accessed from the main class, display() method is accessed from the main class.


Modifier

Class

Package

Subclass

Out side the package

public

Yes

Yes

Yes

Yes

protected

Yes

Yes

Yes

No

Default

Yes

Yes

No

No

Private

Yes

No

No

No



Practical use of Access Modifiers

Access modifiers offer significant benefits for software design and maintenance:


  1. Encapsulation: By controlling the visibility of members, you can encapsulate the internal implementation details of a class, ensuring that its state and behavior are only accessible and modifiable through well-defined interfaces (public members).


  2. Security: Access modifiers help protect sensitive data and functionality by restricting access to authorized classes and parts of your code.


  3. Code Organization: Access modifiers contribute to code organization and maintainability by making it clear which members are intended for external use and which are meant to be internal implementation details.


  4. Inheritance and Polymorphism: Access modifiers are essential for building class hierarchies. They allow you to specify which members can be inherited, overridden, or extended in derived classes.


Non Access Modifiers:

Non access modifiers will provide information about the characteristics of a class, method, variable to the JVM. They are static, final, abstract, synchronized.


Static: static keyword means that the entity to which it is applied is available outside any particular instance of the class, which means the static methods or the attributes are a part of the class and not an object. The memory is allocated to such an attribute or method at the time of class loading.


final: In final keyword indicates that the specific class cannot be extended or a method cannot be overridden.


abstract: abstract keyword is used to declare a class as partially implemented means an object cannot be created directly from the class.


 synchronized: synchronized keyword prevents a block of code from executing by multiple threads at once.


Conclusion:

Access modifiers in java are primarily used for encapsulation. It allows us to control what part of a program modifiers in java primarily used for encapsulation. It allows us to control what part of a program is useful for accessing class members. The different access modifiers in java can prevent the misuse of data.



37 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page