Beruflich Dokumente
Kultur Dokumente
AccessmodifiersWikipedia,thefreeencyclopedia
Accessmodifiers
FromWikipedia,thefreeencyclopedia
Accessmodifiers(oraccessspecifiers)arekeywordsinobjectorientedlanguagesthatsettheaccessibility
ofclasses,methods,andothermembers.Accessmodifiersareaspecificpartofprogramminglanguage
syntaxusedtofacilitatetheencapsulationofcomponents.[1]
InC++,thereareonlythreeaccessmodifiers.C#extendsthenumberofthemtofive,whileJavahasfour
accessmodifiers,[2]butthreekeywordsforthispurpose.InJava,havingnokeywordbeforedefaultstothe
packageprivatemodifier.Accessspecifiersforclasses:Whenaclassisdeclaredaspublic,itisaccessible
tootherclassesdefinedinthesamepackageaswellasthosedefinedinotherpackages.Thisisthemost
commonlyusedspecifierforclasses.Aclasscannotbedeclaredasprivate.Ifnoaccessspecifierisstated,
thedefaultaccessrestrictionswillbeapplied.Theclasswillbeaccessibletootherclassesinthesame
packagebutwillbeinaccessibletoclassesoutsidethepackage.Whenwesaythataclassisinaccessible,it
simplymeansthatwecannotcreateanobjectofthatclassordeclareavariableofthatclasstype.The
protectedaccessspecifiertoocannotbeappliedtoaclass.
Contents
1Namesofkeywords
2ExampleinC++
3References
3.1Notes
3.2Bibliography
Namesofkeywords
C++usesthethreemodifierscalledpublic,protected,andprivate.C#hasthemodifierspublic,protected
,internal,private,andprotectedinternal.Javahaspublic,package,protected,andprivate.Theaccess
modifierpackageisthedefaultandused,ifanyotheraccessmodifierkeywordismissing.Thisisnotthe
caseinC++,whereanabsencemeansthattheobjectisaccessedprivately.Themeaningofthesemodifiers
maydifferfromonelanguagetoanother.Acomparisonofthekeywords,orderedfromthemostrestrictive
tothemostopen,andtheirmeaninginthesethreelanguagesfollows.Theirvisibilityrangesfromthesame
classtothepackagewheretheclassisdefinedtoageneralaccesspermission.Below,themaximalaccessis
writtenintothetable.
http://en.wikipedia.org/wiki/Access_modifiers
1/3
3/18/2015
AccessmodifiersWikipedia,thefreeencyclopedia
Keyword
C#
C++
Getrecommendationsfromyourfriends:
private
class
class
protectedinternal
sameassemblyand
derivedclasses
Java
Facebook
class
protected
derivedclasses
derivedclasses
derivedclasses and/or
withinsamepackage
package
withinitspackage
publicinternal
sameassembly
public
everybody
everybody
everybody
ExampleinC++
#include<iostream>
usingstd::cout;
usingstd::endl;
structB{//defaultaccessmodifierinsidestructispublic
voidset_n(intv){n=v;}
voidf(){cout<<"B::f"<<endl;}
protected:
intm,n;//B::m,B::nareprotected
private:
intx;
};
structD:B{
usingB::m;//D::mispublic
intget_n(){returnn;}//B::nisaccessiblehere,butnotoutside
//intget_x(){returnx;}//ERROR,B::xisinaccessiblehere
private:
usingB::f;//D::fisprivate
};
intmain(){
Dd;
//d.x=2;//ERROR,private
//d.n=2;//ERROR,protected
d.m=2;//protectedB::misaccessibleasD::m
d.set_n(2);//callsB::set_n(int)
cout<<d.get_n()<<endl;//output:2
//d.f();//ERROR,B::fisinaccessibleasD::f
B&b=d;//breferencesdand"views"itasbeingtypeB
//b.x=3;//ERROR,private
//b.n=3;//ERROR,protected
//b.m=3;//ERROR,B::misprotected
b.set_n(3);//callsB::set_n(int)
//cout<<b.get_n();//ERROR,'structB'hasnomembernamed'get_n'
b.f();//callsB::f()
return0;
}
http://en.wikipedia.org/wiki/Access_modifiers
2/3
3/18/2015
AccessmodifiersWikipedia,thefreeencyclopedia
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.
TextisavailableundertheCreativeCommonsAttributionShareAlikeLicenseadditionaltermsmay
apply.Byusingthissite,youagreetotheTermsofUseandPrivacyPolicy.Wikipediaisa
registeredtrademarkoftheWikimediaFoundation,Inc.,anonprofitorganization.
http://en.wikipedia.org/wiki/Access_modifiers
3/3