Access modifiers: IN JAVA (public, private, protected)

0


 Access modifiers: IN JAVA (public, private, protected)



GAIN AND SHINE



Introduction:

    In Java, access modifiers are keywords used to specify the accessibility of classes, fields, methods, and constructors. There are three types of access modifiers in Java: public, private, and protected. Each access modifier has its own scope and restrictions.


    In this blog, we will discuss the three access modifiers in Java, their syntax, and how they are used to control the accessibility of variables, methods, and classes.



Public Access Modifier:

    The public access modifier is the most permissive modifier in Java. It allows classes, methods, and variables to be accessed from anywhere in the program.


The syntax for the public access modifier is:

public <class or method or variable>

Let's look at an example:

public class MyClass {

    public String myString = "Hello World!";

    

    public void printString() {

        System.out.println(myString);

    }

}

    In the example above, we have declared a public class named "MyClass" and a public string variable named "myString". We have also declared a public method named "printString()" which prints the value of "myString".


Private Access Modifier:

    The private access modifier is the most restrictive modifier in Java. It allows only the class in which it is declared to access the variable or method.


The syntax for the private access modifier is:

private <class or method or variable>

Let's look at an example:

public class MyClass {

    private String myString = "Hello World!";

    

    private void printString() {

        System.out.println(myString);

    }

}

    In the example above, we have declared a private string variable named "myString" and a private method named "printString()". These can only be accessed within the class "MyClass".


Protected Access Modifier:

    The protected access modifier allows access to the class in which it is declared, as well as any subclass.


The syntax for the protected access modifier is:

protected <class or method or variable>

Let's look at an example:

public class MyClass {

    protected String myString = "Hello World!";

    

    protected void printString() {

        System.out.println(myString);

    }

}


public class MySubClass extends MyClass {

    public void printStringFromSubclass() {

        System.out.println(myString);

    }

}

    In the example above, we have declared a protected string variable named "myString" and a protected method named "printString()". These can be accessed within the class "MyClass" and any subclass of "MyClass". We have also created a subclass named "MySubClass" which can access the protected variable and method of "MyClass".



Conclusion:

    Access modifiers are an important part of Java programming as they help in encapsulation and abstraction of code. Public access modifier provides full accessibility of a variable, method, or class to other classes, private access modifier restricts the accessibility of a variable, method, or class to within the same class, while protected access modifier provides accessibility to the class and its subclasses. It is important to use access modifiers appropriately in order to maintain the integrity of your code and prevent unwanted access to your data. By using access modifiers correctly, you can create robust, secure, and maintainable Java applications.




Post a Comment

0Comments

Share Your Feedback Here !!

Post a Comment (0)