AppDividend
Latest Code Tutorials

Access Modifiers in Java Example | Java Access Modifiers

0

Access Modifiers in Java Example | Java Access Modifiers is today’s topic. We can understand this topic through the meaning of this word. Access means to approach, and Access Modifiers are those keywords which define the namespace of variable, methods, constructors, classes, etc. Access Modifier is the keywords in object-oriented languages, which makes to understand who can approach that particular Data member, method, Class, etc.

Access Modifiers in Java

As a name suggests, access modifiers in Java language help to restrict a scope of the class, constructor, variable, method, or the data member. There are four types of access modifiers available in Java language.

  1. Default – No keyword required: Visible to the package
  2. Private: Visible to the class only 
  3. Protected: Visible to the package and all the subclasses
  4. Public: Visible to the world

 

Access Modifiers in Java

#Public Keyword in Java

Keyword public uses to indicate the public modifier. And public modifier indicates that the entire class can access the given variable or method. It is also possible to access it by other packages, classes, subclasses.

Mostly we use public specifiers, so it has the most extensive scope among all the access modifiers.

Our main function is always public and cannot be private or any other.

class Java
{
     String s=new String("\nWelcome to Appdivident\n");
}
class publ
{
   public static void main(String args[ ])
   {                 
      Java ob=new Java();
      System.out.println(ob.s);
   }                 
}

See the following output.

 

public

From the above output that we have to use public specifier and used that class in another class in a different package.

#Private Keyword in Java

Keyword Private uses to indicate the private modifier. Its class can access it. Other packages and subclasses cannot access it in its class. It somewhat behaves like the final keyword. Most restricted class in all the classes. Interfaces can’t be private.

Private means “only visible within the enclosing class.”

See the following program.

import java.io.*;
class A
{
        private void display()
	{
	  System.out.println("Hello, Welcome to appdividend");
	}
}
class priv
{
    public static void main(String args[ ])
    {
       A obj=new A();
       obj.display();
    }        
}

See the following output.

 

private

In this example, we are not able to access private variables. So it has shown the error, and it gives us proof.

#Protected Keyword in Java

Keyword Protected is used to indicate the protected modifier and can be accessed by all classes and subclasses of the same package and also by the subclasses of different packages.

Non-subclasses of other packages cannot access that. Protected access modifier also can’t be used by classes and interfaces, and struct members also can’t be protected as it doesn’t promote inheritance.

It is someway similar to the default access modifier, but the difference is that subclasses can access it.

The methods or data members declared as protected are accessible within the same package or subclasses in a different package.

See the following code.

import java.io.*;
class A
{
        protected void display()
	{
	  System.out.println("\nHello, Welcome to appdivident\n");
	}
}
class priv
{
    public static void main(String args[ ])
    {
       A obj=new A();
       obj.display();
    }        
}

See the following output.

 

protected

In the above example, we are not able to access the protected variables.

So it has shown the error, and it gives us proof.

#Default Keyword in Java

No Keyword uses to indicate default. When we do not notice anything, there will be default modifier. The members did not declare using access specifiers can be accessed only within the same package. We can’t use the default for methods within an interface.

The data members, class or methods which are not declared using any access modifiers, i.e., having default access modifier are accessible only within the same package.

See the following code.

class Java
{
       int a=541;
}
class Main
{
    public static void main(String args[ ])
    {
                 
      Java ob = new Java();
      System.out.println("\nValue of a is "+ob.a);
    }       
}

See the following output.

Main

 In the above example, we are not able to access the default.

So it has shown the error, and it gives us proof.

 

Java Access Modifiers

#Need for Access Modifiers

  1. They allow maintaining the security and privacy of a class.
  2. It is used to facilitate the encapsulation of the program.

#Access Control and Inheritance

The following rules for inherited methods are enforced.

  1. Methods that are declared public in the superclass also must be public in all subclasses.
  2. Methods that are declared protected in the superclass must either be protected or the public in subclasses; they cannot be private.
  3. Methods that are declared private are not inherited at all, so there is no rule for them.

#Non-Access Modifiers

For classes, you can use either final or abstract:

Modifier Description
final The class cannot be inherited by the other classes.
abstract The class cannot be used to create the objects (If we want to access the abstract class, it must be inherited from the another class. You will learn more about the inheritance in the Inheritance chapter)

 

Finally, Access Modifiers in Java Example | Java Access Modifiers Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.