access modifiers - wikipedia, the free encyclopedia

Post on 02-Oct-2015

221 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

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.

top related