Knowledge (XXG)

Encapsulation (computer programming)

Source 📝

301: 176:
is generally hidden outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields. Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent
34:
refers to the bundling of data with the mechanisms or methods that operate on the data. It may also refer to the limiting of direct access to some of that data, such as an object's components. Essentially, encapsulation prevents external code from being concerned with the internal workings of an
153:
and encapsulation at length and state that in their experience, designers overuse inheritance. They claim that inheritance often breaks encapsulation, given that inheritance exposes a subclass to the details of its parent's implementation. As described by the
50:
Encapsulation also encourages programmers to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy comprehension by other programmers. Encapsulation is a technique that encourages
732:. The contents of this type are known and accessible only to the implementation of the API functions; clients cannot directly access its contents. The source code for these functions defines the actual contents of the structure: 38:
Encapsulation allows developers to present a consistent interface that is independent of its internal implementation. As one example, encapsulation can be used to hide the values or state of a structured data object inside a
612:, for example, a structure can be declared in the public API via the header file for a set of functions that operate on an item of data containing data members that are not accessible to clients of the API with the 1232:
Encapsulation mechanisms enable the programmer to group data and the subroutines that operate on them together in one place, and to hide irrelevant details from the users of an abstraction
124:
The second definition reflects that in many object-oriented languages, and other related fields, the components are not hidden automatically and this can be overridden. Thus,
232:" and that they do not "hide any information". Information hiding is accomplished by furnishing a compiled version of the source code that is interfaced via a header file. 916:, which does not support variable access restrictions. However, the convention is that a variable whose name is prefixed by an underscore should be considered private. 1091: 150: 109:
Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of
1368: 1339: 1314: 1281: 1253: 1225: 87: 1482: 90:, and other related fields, encapsulation refers to one of two related but distinct notions, and sometimes to the combination thereof: 1463: 1408: 43:. This prevents clients from directly accessing this information in a way that could expose hidden implementation details or violate 236: 172:
Under the definition that encapsulation "can be used to hide data members and member functions", the internal representation of an
1487: 1304: 52: 1156: 1382: 1380: 307: 277: 197: 114: 913: 293: 244: 201: 178: 102: 1130: 289: 132: 40: 1096: 723:// extern defines functions that can be called outside the current file, the default behavior even without the keyword 463: 281: 205: 189: 110: 59: 252: 173: 95: 1330:
Connolly, Thomas M.; Begg, Carolyn E. (2005). "Ch. 25: Introduction to Object DMBS § Object-oriented concepts".
609: 44: 1101: 256: 74:
also offer encapsulation. The similarity has been explained by programming language theorists in terms of
177:
state. A supposed benefit of encapsulation is that it can reduce system complexity, and thus increase
67: 158:, overuse of inheritance and therefore encapsulation, can become too complicated and hard to debug. 71: 63: 1300: 167: 125: 62:(OOP) systems support encapsulation, but encapsulation is not unique to OOP. Implementations of 17: 1459: 1404: 1364: 1335: 1310: 1277: 1249: 1221: 1180: 229: 75: 1269: 729: 1425: 145: 181:, by allowing the developer to limit the interdependencies between software components. 1389:, pp. 73–77, Chapter §4 Item 15 Minimize the accessibility of classes and members. 1106: 1476: 1357: 907: 728:
Clients call the API functions to allocate, operate on, and deallocate objects of an
240: 208:) offer the programmer some control over what is hidden, typically via keywords like 155: 118: 1332:
Database systems: a practical approach to design, implementation, and management
300: 235:
Almost always, there is a way to override such protection – usually via
310:
that shows how access to a data field can be restricted through the use of a
1135: 448:* "CheckBalance" method provided by the "Account" class 185: 135:
in most object-oriented languages, although other alternatives also exist.
128:
is defined as a separate notion by those who prefer the second definition.
1355:
Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994).
451:* but it cannot manipulate the value of "accountBalance" */ 608:
Encapsulation is also possible in non-object-oriented languages. In
101:
A language construct that facilitates the bundling of data with the
273: 193: 94:
A language mechanism for restricting direct access to some of the
285: 192:
only allow access via object methods, but most others (e.g.,
720:// extern keywords here are redundant, but don't hurt. 1403:(2nd ed.). W. W. Norton & Company. p. 464. 636:// API functions that operate on 'Entity' objects 445:/* This Main method can check the balance via the public 259:) are an exception, and guarantee strong encapsulation. 239:
API (Ruby, Java, C#, etc.), sometimes by mechanism like
1181:"Encapsulation in Object Oriented Programming (OOPS)" 1248:(2nd ed.). Jones & Bartlett. p. 396. 1356: 131:The features of encapsulation are supported using 1050:# This will print 'Maximum speed is 200.' 1334:(4th ed.). Pearson Education. p. 814. 1080:# This will print 'Maximum speed is 10.' 117:view encapsulation as a feature of the language 113:, while some programming languages that provide 296:offer ways to restrict access to data fields. 105:(or other functions) operating on those data. 8: 1456:"Effective Java: Programming Language Guide" 1220:(2 ed.). Morgan Kaufmann. p. 481. 1157:"What is Object-Oriented Programming (OOP)?" 1276:. Cambridge University Press. p. 522. 1206:, § 24.2 Data Abstraction with Existentials 1124: 1122: 251:in C++. Systems that provide object-level 1246:Programming and problem solving with Java 1131:"Encapsulation is not information hiding" 1092:Inheritance (object-oriented programming) 1295: 1293: 737:// Implementation file "api.c" 1118: 633:// Opaque structure with hidden members 1426:"The Meaning of Underscores in Python" 1203: 47:invariance maintained by the methods. 1386: 88:object-oriented programming languages 7: 1244:Dale, Nell B.; Weems, Chip (2007). 1458:(third ed.). Addison-Wesley. 25: 1274:Concepts in programming languages 247:), or special keyword usage like 1401:C Programming: A Modern Approach 1129:Rogers, Wm. Paul (18 May 2001). 621:// Header file "api.h" 299: 18:Encapsulation (computer science) 1306:Types and Programming Languages 1218:Programming language pragmatics 797:// API function implementations 1: 216:. ISO C++ standard refers to 139:Encapsulation and inheritance 149:discuss the tension between 1483:Object-oriented programming 1216:Scott, Michael Lee (2006). 1097:Object-oriented programming 111:object-oriented programming 60:object-oriented programming 1504: 1430:Improve Your Python Skills 1309:. MIT Press. p. 266. 905: 165: 253:capability-based security 918: 734: 618: 468: 316: 1185:www.enjoyalgorithms.com 1102:Software design pattern 1002:"Maximum speed is 912:Below is an example of 462:Below is an example in 306:Below is an example in 268:Restricting data fields 257:object-capability model 121:to object orientation. 1488:Programming principles 1454:Bloch, Joshua (2018). 30:In software systems, 1399:King, K. N. (2008). 184:Some languages like 64:abstract data types 1363:. Addison-Wesley. 168:Information hiding 162:Information hiding 126:information hiding 1370:978-0-201-63361-0 1341:978-0-321-21025-8 1316:978-0-262-16209-8 1283:978-0-521-78098-8 1270:Mitchell, John C. 1255:978-0-7637-3402-2 1227:978-0-12-633951-2 743:"api.h" 255:(adhering to the 230:access specifiers 76:existential types 16:(Redirected from 1495: 1469: 1441: 1440: 1438: 1436: 1421: 1415: 1414: 1396: 1390: 1384: 1375: 1374: 1362: 1352: 1346: 1345: 1327: 1321: 1320: 1301:Pierce, Benjamin 1297: 1288: 1287: 1266: 1260: 1259: 1241: 1235: 1234: 1213: 1207: 1201: 1195: 1194: 1192: 1191: 1177: 1171: 1170: 1168: 1167: 1161:App Architecture 1153: 1147: 1146: 1144: 1143: 1126: 1081: 1078: 1075: 1072: 1069: 1066: 1063: 1060: 1057: 1054: 1051: 1048: 1045: 1042: 1039: 1036: 1033: 1030: 1027: 1024: 1021: 1018: 1015: 1012: 1009: 1006: 1003: 1000: 997: 994: 991: 988: 985: 982: 979: 976: 973: 970: 967: 964: 961: 958: 955: 952: 949: 946: 943: 940: 937: 934: 931: 928: 925: 922: 897: 894: 891: 888: 885: 882: 879: 876: 873: 870: 867: 864: 861: 858: 855: 852: 849: 846: 843: 840: 837: 834: 831: 828: 825: 822: 819: 816: 813: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 730:opaque data type 724: 721: 718: 715: 712: 709: 706: 703: 700: 697: 694: 691: 688: 685: 682: 679: 676: 673: 670: 667: 664: 661: 658: 655: 652: 649: 646: 643: 640: 637: 634: 631: 628: 625: 622: 615: 604: 601: 598: 595: 592: 589: 586: 583: 580: 577: 574: 571: 568: 565: 562: 559: 556: 553: 550: 547: 544: 541: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 487: 484: 481: 478: 475: 472: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 428: 425: 422: 419: 416: 413: 410: 407: 404: 401: 398: 395: 392: 389: 386: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 313: 303: 250: 227: 223: 219: 215: 211: 115:lexical closures 27:Bundling of data 21: 1503: 1502: 1498: 1497: 1496: 1494: 1493: 1492: 1473: 1472: 1466: 1453: 1450: 1445: 1444: 1434: 1432: 1423: 1422: 1418: 1411: 1398: 1397: 1393: 1385: 1378: 1371: 1359:Design Patterns 1354: 1353: 1349: 1342: 1329: 1328: 1324: 1317: 1299: 1298: 1291: 1284: 1268: 1267: 1263: 1256: 1243: 1242: 1238: 1228: 1215: 1214: 1210: 1202: 1198: 1189: 1187: 1179: 1178: 1174: 1165: 1163: 1155: 1154: 1150: 1141: 1139: 1128: 1127: 1120: 1115: 1088: 1083: 1082: 1079: 1076: 1073: 1070: 1067: 1064: 1061: 1058: 1055: 1052: 1049: 1046: 1043: 1040: 1037: 1034: 1031: 1028: 1025: 1022: 1019: 1016: 1013: 1010: 1007: 1004: 1001: 998: 995: 992: 989: 986: 983: 980: 977: 974: 971: 968: 965: 962: 959: 956: 953: 950: 947: 944: 941: 938: 935: 932: 929: 926: 923: 920: 910: 904: 899: 898: 895: 892: 889: 886: 883: 880: 877: 874: 871: 868: 865: 862: 859: 856: 853: 850: 847: 844: 841: 838: 835: 832: 829: 826: 823: 820: 817: 814: 811: 808: 805: 802: 799: 796: 793: 790: 787: 784: 781: 778: 775: 772: 769: 766: 763: 760: 757: 754: 751: 748: 745: 742: 739: 736: 726: 725: 722: 719: 716: 713: 710: 707: 704: 701: 698: 695: 692: 689: 686: 683: 680: 677: 674: 671: 668: 665: 662: 659: 656: 653: 650: 647: 644: 641: 638: 635: 632: 629: 626: 623: 620: 613: 606: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 460: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 379:_accountBalance 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 346:_accountBalance 345: 342: 339: 336: 333: 330: 327: 324: 321: 318: 311: 272:Languages like 270: 265: 248: 225: 221: 217: 213: 209: 170: 164: 146:Design Patterns 143:The authors of 141: 84: 28: 23: 22: 15: 12: 11: 5: 1501: 1499: 1491: 1490: 1485: 1475: 1474: 1471: 1470: 1465:978-0134685991 1464: 1449: 1446: 1443: 1442: 1416: 1410:978-0393979503 1409: 1391: 1376: 1369: 1347: 1340: 1322: 1315: 1289: 1282: 1261: 1254: 1236: 1226: 1208: 1196: 1172: 1148: 1117: 1116: 1114: 1111: 1110: 1109: 1107:Facade pattern 1104: 1099: 1094: 1087: 1084: 919: 906:Main article: 903: 900: 836:process_entity 735: 672:process_entity 619: 469: 317: 269: 266: 264: 261: 166:Main article: 163: 160: 140: 137: 107: 106: 99: 98:'s components. 83: 80: 26: 24: 14: 13: 10: 9: 6: 4: 3: 2: 1500: 1489: 1486: 1484: 1481: 1480: 1478: 1467: 1461: 1457: 1452: 1451: 1447: 1431: 1427: 1420: 1417: 1412: 1406: 1402: 1395: 1392: 1388: 1383: 1381: 1377: 1372: 1366: 1361: 1360: 1351: 1348: 1343: 1337: 1333: 1326: 1323: 1318: 1312: 1308: 1307: 1302: 1296: 1294: 1290: 1285: 1279: 1275: 1271: 1265: 1262: 1257: 1251: 1247: 1240: 1237: 1233: 1229: 1223: 1219: 1212: 1209: 1205: 1200: 1197: 1186: 1182: 1176: 1173: 1162: 1158: 1152: 1149: 1138: 1137: 1132: 1125: 1123: 1119: 1112: 1108: 1105: 1103: 1100: 1098: 1095: 1093: 1090: 1089: 1085: 917: 915: 909: 908:Name mangling 902:Name mangling 901: 733: 731: 617: 611: 467: 465: 315: 309: 304: 302: 297: 295: 291: 287: 283: 279: 275: 267: 262: 260: 258: 254: 246: 242: 241:name mangling 238: 233: 231: 207: 203: 199: 195: 191: 187: 182: 180: 175: 169: 161: 159: 157: 156:yo-yo problem 152: 148: 147: 138: 136: 134: 129: 127: 122: 120: 116: 112: 104: 100: 97: 93: 92: 91: 89: 81: 79: 77: 73: 69: 65: 61: 56: 54: 48: 46: 42: 36: 33: 32:encapsulation 19: 1455: 1433:. Retrieved 1429: 1424:Bader, Dan. 1419: 1400: 1394: 1358: 1350: 1331: 1325: 1305: 1273: 1264: 1245: 1239: 1231: 1217: 1211: 1199: 1188:. Retrieved 1184: 1175: 1164:. Retrieved 1160: 1151: 1140:. Retrieved 1134: 911: 869:close_entity 764:// ID number 727: 699:close_entity 607: 461: 439:CheckBalance 367:CheckBalance 305: 298: 271: 234: 183: 171: 144: 142: 130: 123: 108: 85: 57: 49: 37: 31: 29: 1204:Pierce 2002 809:open_entity 651:open_entity 151:inheritance 1477:Categories 1448:References 1435:1 November 1387:Bloch 2018 1190:2024-03-02 1166:2024-03-02 1142:2020-07-20 579:BigDecimal 513:BigDecimal 498:BigDecimal 486:BigDecimal 237:reflection 179:robustness 119:orthogonal 53:decoupling 1136:JavaWorld 1113:Citations 1059:_maxspeed 1014:_maxspeed 960:_maxspeed 616:keyword. 594:getSalary 516:getSalary 433:myAccount 427:myBalance 409:myAccount 314:keyword: 218:protected 186:Smalltalk 72:libraries 1303:(2002). 1272:(2003). 1086:See also 933:__init__ 770:ent_name 740:#include 573:Employee 561:Employee 504:50000.00 477:Employee 263:Examples 35:object. 1020:." 788:members 776:// Name 483:private 424:decimal 418:Account 406:Account 364:decimal 343:decimal 340:private 334:Account 322:Program 312:private 222:private 214:private 133:classes 103:methods 82:Meaning 68:modules 1462:  1407:  1367:  1338:  1313:  1280:  1252:  1224:  1068:redcar 1053:redcar 1038:redcar 1026:redcar 914:Python 878:Entity 875:struct 845:Entity 842:struct 803:Entity 800:struct 758:ent_id 749:Entity 746:struct 708:Entity 705:struct 693:extern 681:Entity 678:struct 666:extern 645:Entity 642:struct 639:extern 627:Entity 624:struct 614:extern 546:static 543:public 534:salary 525:return 510:public 489:salary 471:public 391:static 376:return 361:public 352:500.00 328:public 294:Delphi 292:, and 249:friend 245:Python 226:public 210:public 202:Delphi 174:object 96:object 70:, and 1074:drive 1044:drive 993:print 984:-> 972:drive 945:-> 921:class 785:other 474:class 331:class 319:class 290:Swift 45:state 41:class 1460:ISBN 1437:2019 1405:ISBN 1365:ISBN 1336:ISBN 1311:ISBN 1278:ISBN 1250:ISBN 1222:ISBN 1008:self 987:None 978:self 954:self 948:None 939:self 884:info 866:void 851:info 767:char 714:info 696:void 687:info 552:main 549:void 528:this 464:Java 397:Main 394:void 282:Java 228:as " 224:and 212:and 206:Java 190:Ruby 188:and 58:All 1032:Car 969:def 966:200 930:def 924:Car 893:... 860:... 833:int 827:... 815:int 791:... 782:and 779:... 755:int 669:int 657:int 597:(); 582:sal 576:(); 570:new 495:new 442:(); 421:(); 415:new 286:PHP 274:C++ 204:or 194:C++ 86:In 1479:: 1428:. 1379:^ 1292:^ 1230:. 1183:. 1159:. 1133:. 1121:^ 1077:() 1065:10 1047:() 1035:() 818:id 794:}; 717:); 690:); 663:); 660:id 555:() 519:() 507:); 466:: 400:() 370:() 308:C# 288:, 284:, 280:, 278:C# 276:, 220:, 200:, 198:C# 196:, 78:. 66:, 55:. 1468:. 1439:. 1413:. 1373:. 1344:. 1319:. 1286:. 1258:. 1193:. 1169:. 1145:. 1071:. 1062:= 1056:. 1041:. 1029:= 1023:) 1017:} 1011:. 1005:{ 999:f 996:( 990:: 981:) 975:( 963:= 957:. 951:: 942:) 936:( 927:: 896:} 890:{ 887:) 881:* 872:( 863:} 857:{ 854:) 848:* 839:( 830:} 824:{ 821:) 812:( 806:* 773:; 761:; 752:{ 711:* 702:( 684:* 675:( 654:( 648:* 630:; 610:C 603:} 600:} 591:. 588:e 585:= 567:= 564:e 558:{ 540:} 537:; 531:. 522:{ 501:( 492:= 480:{ 457:} 454:} 436:. 430:= 412:= 403:{ 388:} 385:} 382:; 373:{ 358:; 355:m 349:= 337:{ 325:{ 243:( 20:)

Index

Encapsulation (computer science)
class
state
decoupling
object-oriented programming
abstract data types
modules
libraries
existential types
object-oriented programming languages
object
methods
object-oriented programming
lexical closures
orthogonal
information hiding
classes
Design Patterns
inheritance
yo-yo problem
Information hiding
object
robustness
Smalltalk
Ruby
C++
C#
Delphi
Java
access specifiers

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.