access modifiers - wikipedia, the free encyclopedia

3
Access modifiers From Wikipedia, the free encyclopedia Access modifiers (or access specifiers) are keywords in objectoriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components. [1] In C++, there are only three access modifiers. C# extends the number of them to five, while Java has four access modifiers, [2] but three keywords for this purpose. In Java, having no keyword before defaults to the packageprivate modifier. Access specifiers for classes: When a class is declared as public, it is accessible to other classes defined in the same package as well as those defined in other packages. This is the most commonly used specifier for classes. A class cannot be declared as private. If no access specifier is stated, the default access restrictions will be applied. The class will be accessible to other classes in the same package but will be inaccessible to classes outside the package. When we say that a class is inaccessible, it simply means that we cannot create an object of that class or declare a variable of that class type. The protected access specifier too cannot be applied to a class. Contents 1 Names of keywords 2 Example in C++ 3 References 3.1 Notes 3.2 Bibliography Names of keywords C++ uses the three modifiers called public, protected, and private. C# has the modifiers public, protected ,internal, private, and protected internal. Java has public, package, protected, and private. The access modifier package is the default and used, if any other access modifier keyword is missing. This is not the case in C++, where an absence means that the object is accessed privately. The meaning of these modifiers may differ from one language to another. A comparison of the keywords, ordered from the most restrictive to the most open, and their meaning in these three languages follows. Their visibility ranges from the same class to the package where the class is defined to a general access permission. Below, the maximal access is written into the table.

Upload: rameshctlatp

Post on 02-Oct-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

  • 3/18/2015 AccessmodifiersWikipedia,thefreeencyclopedia

    http://en.wikipedia.org/wiki/Access_modifiers 1/3

    AccessmodifiersFromWikipedia,thefreeencyclopedia

    Accessmodifiers(oraccessspecifiers)arekeywordsinobjectorientedlanguagesthatsettheaccessibilityofclasses,methods,andothermembers.Accessmodifiersareaspecificpartofprogramminglanguagesyntaxusedtofacilitatetheencapsulationofcomponents.[1]

    InC++,thereareonlythreeaccessmodifiers.C#extendsthenumberofthemtofive,whileJavahasfouraccessmodifiers,[2]butthreekeywordsforthispurpose.InJava,havingnokeywordbeforedefaultstothepackageprivatemodifier.Accessspecifiersforclasses:Whenaclassisdeclaredaspublic,itisaccessibletootherclassesdefinedinthesamepackageaswellasthosedefinedinotherpackages.Thisisthemostcommonlyusedspecifierforclasses.Aclasscannotbedeclaredasprivate.Ifnoaccessspecifierisstated,thedefaultaccessrestrictionswillbeapplied.Theclasswillbeaccessibletootherclassesinthesamepackagebutwillbeinaccessibletoclassesoutsidethepackage.Whenwesaythataclassisinaccessible,itsimplymeansthatwecannotcreateanobjectofthatclassordeclareavariableofthatclasstype.Theprotectedaccessspecifiertoocannotbeappliedtoaclass.

    Contents

    1Namesofkeywords2ExampleinC++3References

    3.1Notes3.2Bibliography

    Namesofkeywords

    C++usesthethreemodifierscalledpublic,protected,andprivate.C#hasthemodifierspublic,protected,internal,private,andprotectedinternal.Javahaspublic,package,protected,andprivate.Theaccessmodifierpackageisthedefaultandused,ifanyotheraccessmodifierkeywordismissing.ThisisnotthecaseinC++,whereanabsencemeansthattheobjectisaccessedprivately.Themeaningofthesemodifiersmaydifferfromonelanguagetoanother.Acomparisonofthekeywords,orderedfromthemostrestrictivetothemostopen,andtheirmeaninginthesethreelanguagesfollows.Theirvisibilityrangesfromthesameclasstothepackagewheretheclassisdefinedtoageneralaccesspermission.Below,themaximalaccessiswrittenintothetable.

  • 3/18/2015 AccessmodifiersWikipedia,thefreeencyclopedia

    http://en.wikipedia.org/wiki/Access_modifiers 2/3

    Keyword C# C++ Javaprivate class class class

    protectedinternalsameassemblyandderivedclasses

    protected derivedclasses derivedclassesderivedclassesand/orwithinsamepackage

    package withinitspackagepublicinternal sameassembly public everybody everybody everybody

    ExampleinC++

    #includeusingstd::cout;usingstd::endl;structB{//defaultaccessmodifierinsidestructispublicvoidset_n(intv){n=v;}voidf(){cout

  • 3/18/2015 AccessmodifiersWikipedia,thefreeencyclopedia

    http://en.wikipedia.org/wiki/Access_modifiers 3/3

    References

    Notes

    1. Mayo2002,pp.199.2. http://javapapers.com/corejava/accessmodifiersinjavaexplain/

    Bibliography

    Mayo,Joseph(2002),CSharpUnleashed(http://books.google.co.uk/books?id=XdHYaEIl9gC),Samspublishing,ISBN9780672321221

    Retrievedfrom"http://en.wikipedia.org/w/index.php?title=Access_modifiers&oldid=647633207"

    Categories: Programmingconstructs Computingstubs

    Thispagewaslastmodifiedon18February2015,at00:23.TextisavailableundertheCreativeCommonsAttributionShareAlikeLicenseadditionaltermsmayapply.Byusingthissite,youagreetotheTermsofUseandPrivacyPolicy.WikipediaisaregisteredtrademarkoftheWikimediaFoundation,Inc.,anonprofitorganization.