Java Program using private access modifier

Q. Write a program to use private access modifier in Java.

Answer:

Private:
The private methods, variables and constructor are not accessible to any other class. It is the most restrictive access modifier. A class except a nested class cannot be private. 

Private.java

class Private
{
    private int id = 101;
    private String name = "Smith";
    private void show()
    {
        System.out.println("Id = "+id+"\nName = "+name);
    }
    public static void main(String args[])
    {
        Private pvt = new Private();
        pvt.show();
        System.out.println("Id: "+pvt.id+" Name: "+pvt.name);
    }
}


Output:

private access modifier