Access Specifiers

View previous topic View next topic Go down

Access Specifiers

Post by Sujith on Tue Jul 06, 2010 7:42 pm

Access specifiers

Access specifiers
or Access modifiers are keywords that restrict access to certain types of data. In Java, there are 4 types of access specifiers:

1. public - Specified data can be accessed by:

  • Current Class
  • Classes in Current Package
  • Sub Classes
  • Classes in Other Package





2. protected - Specified data can be accessed by:

  • Current Class
  • Classes in Current Package
  • Sub Classes





3. no modifer - Specified data can be accessed by:

  • Current Class
  • Classes in Current Package





4. private - Specified data can be accessed by:

  • Current Class



This is more easily represented as a table:


Access Modifiers

Same ClassSame PackageSubclassOther packages
publicYYYY
protectedYYYN
no access modifierYYNN
privateYNNN




Now let's look on how to use them. Since in the ICSE syllabus only the 'public' keyword is necessary, that is what I'll show you:
Code:

class PublicSpecifierDemo
{
    public double piValue=3.14;      // This Integer can be used by all classes.
    // Even public methods are present
    public double getData()
    {
        return piValue;
    }
    public static void main(String[] args)
    {
    // Even Objects
    public PublicSpecifierDemo obj;
    obj = new PublicSpecifierDemo();
    System.out.print(obj.piValue);  // Prints 3.14
    System.out.print(obj.getData);  // Prints 3.14
    }
}


This completes the tutorial. If you have any doubts contact me.

______________________________________________________
It is the simplest things that are most Complicated!
Been on this forum since 2607 days!

avatar
Sujith
LSF Expert
LSF Expert

Posts : 126

http://osalfps.org

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum