Also view this page |
Class Attribute |
Access privilages |
No access attribute |
Can be accessed from the methods in any class in the
same package |
public |
Accessible to all the members or instances |
private |
Accessible from methods inside the same class |
protected |
Accessible from methods in the same package and from any
subclass |
|
The image below shows, how to use NetBean 5.5 GUI to create an
application, and the scope of the class type and methods. |
|
Now note in the above image, we have only do_something() constructor and
at the next step shown below, I added another constructor. Also note the ""
next to the private members, that inidicates that these members can only be
access by other class members in the same, but not by the derived or
external or inherited classes or across the classes. |
protected members
|
Now let us look at the instances and objects of the class "do_something"
and the methods accessible through an object of "do_something". The Netbean
GUI (or other GUIs), allows to select all the methods and fields accessible
via the instance of a class created in the methods of the client
class. Note private members did not show up in the selection pane.
The protected members can only be accessed via instances. If you have created an
object that does not
|
import java.util.*;
class do_something
{
do_something() {
System.out.println("default constructor");
}
do_something(String str) {
System.out.println("String constructor");
}
public int n1 = 12;
private int n2 = 10;
protected int n3 = 15;
public void print_something()
{
System.out.println("public printer");
private_printer("calling via public");
}
private void private_printer(String str)
{
System.out.println("private printer = " + str);
}
void calcuate()
{
System.out.println( 2 + 2);
}
}
public class Main {
public static void main(String[] args) {
// TODO code application logic here
do_something ds = new do_something();
System.out.println(" ds.n1 = " + ds.n1 + " ds.n3 = " + ds.n3);
ds.print_something();
}
}Result: ( Using NetBean 5.5)
default constructor
ds.n1 = 12 ds.n3 = 15
public printer
private printer = calling via public
|
Just like C#, java 5 up does not care about protected members?? |
|
|
|
|
|
|
|
|
|
|
|
|
|