Core Java - SPLessons

Java Access Modifiers

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Access Modifiers

Java Access Modifiers

shape Description

Java Access Modifiers, The modifiers of the class can be provided access by using the access specifiers. The Java Access Modifiers define the visibility level of the classes, methods, constructor. Java provides 4 Java Access Modifiers as follows.

shape Conceptual Figure

shape Public

The public members can be referred from anywhere. I wanted to access public members of a class from outside the package, the class must be public. The scope of public modifier is global. Following is an example. forloop.java [java]public class forloop { public static void main(String[] args) { for(int i=1;i<=10;i++){ System.out.println(i); } } }[/java] Output When compile the code output will be as follows. [java]1 2 3 4 5 6 7 8 9 10 [/java]

shape Private

Private members of a class will not available to same package class as well as other packages class, the scope of private variable is with in the class. Following is an example. [java] class A{ private int data=78; private void msg(){System.out.println("Hello SPlessons");} } public class Simple{ public static void main(String args[]){ A obj=new A(); System.out.println(obj.data);//Compile Time Error obj.msg();//Compile Time Error } }[/java] Here developer created two classes A and Simple. In class A, one private data member and one private method are there at the same time developer is calling those from outside the class, then the developer will get compile time error as shown in the above example.

shape Protected

Protected members of a class will available to same package class will not available to other package but, there should be a relation between the classes by the way of inheritance. Following is an example. A.java [java]package pack; public class A{ protected void splesson(){System.out.println("Hello Splessons");} } [/java] Here created a class with the name A and given specifier as protected to the method, Hello Splessons is the message will printed from the second class. B.java [java]package mypack; import pack.*; class B extends A{ public static void main(String args[]){ B obj = new B(); obj.splesson(); } } [/java] Output When compile the code output will be as follows. [java]Hello Splessons[/java]

shape Default

Protected members of a class will available to same package class will not available to other package, the scope of default specifier id package.

Difference between default and protected

Default is no-modifier. i.e when client don't indicate any get to modifier expressly for a variable or a class it gets the default get to. Default get to likewise signifies "package level" get to. That implies a default member can be gotten to just inside a similar package in which the member is declared. Protected modifier is the somewhat precarious and user can state is a superset of the default modifier. Protected members are same as the default members to the extent the access in a similar package. The distinction is that, the protected members are likewise open to the subclasses of the class in which the class is announced which are outside the package in which the parent class is available. By using inheritance concept protected members are accessible for outside package also.

Summary

shape Key Points

  • Java Access Modifiers - The default and protected access modifier functionality is same, but protected needs inheritance relation between the classes.
  • Java Access Modifiers - The scope of public specifier is global.