{"id":1342,"date":"2018-11-23T07:13:11","date_gmt":"2018-11-23T15:13:11","guid":{"rendered":"http:\/\/www.hybridclassroom.com\/blog\/?p=1342"},"modified":"2018-11-23T07:13:11","modified_gmt":"2018-11-23T15:13:11","slug":"demo-encapsulation","status":"publish","type":"post","link":"https:\/\/www.hybridclassroom.com\/blog\/?p=1342","title":{"rendered":"Demo: Encapsulation"},"content":{"rendered":"<p>The idea of encapsulation is fundamental to computers in a number of ways. Generally speaking, &#8220;encapsulation&#8221; refers to the idea of building a container around something, as if that thing were contained in a capsule. When it comes to computers, there are a couple of slightly different ways the term might be used.<\/p>\n<h2>Hiding code in a class, function, or library<\/h2>\n<p>Commonly in computer programming, encapsulation refers to the idea of hiding away the details of code. For example, you may write a bit of Python code that takes three coefficients for a quadratic equation a, b, and c, and calculates the real roots (solutions) of that equation:<\/p>\n<pre>a = 2\r\nb = 4\r\nc = -5\r\nroot1 = (-b + ((b * b) - (4 * a * c)) ** (1 \/ 2)) \/ (2 * a)\r\nroot2 = (-b - ((b * b) - (4 * a * c)) ** (1 \/ 2)) \/ (2 * a)<\/pre>\n<p>If you were having to write lots of programs to calculate quadratic roots, or if you wanted to be able to use those lines of code other places, you might very well write them into a small function or method called something like <code>quadratic_solver<\/code> that you could use in lots of different places. You might call that function like this:<\/p>\n<p><code>roots = quadratic_solver(2, 4, -5)<\/code><\/p>\n<p>In this single line of code, the messy details of multiplying, dividing, and square-rooting have all been hidden away from the main program. The details of that calculation have been &#8220;encapsulated&#8221; in the function.<\/p>\n<p>Any time you import a library into your program&#8211;<code>import java.util.Scanner;<\/code> in Java, for example, or <code>import random<\/code> in Python&#8211;you are bringing in complicated bits of code that will be available for you to use without having to worry about some of the arcane, or mundane, or complex details that are contained in that code. The concept of encapsulation is extraordinarily powerful.<\/p>\n<h2>Programming languages: Machine code, Assembly, High-level<\/h2>\n<p>A second way of considering encapsulation is less obvious, but one that we use all the time.<\/p>\n<h3>High-level languages<\/h3>\n<p>You have almost certainly seen computer programs written in some &#8220;high-level&#8221; language like Python or Java. These languages are called high-level because they, for the most part, consists of syntax that might be recognized and understood.<\/p>\n<p>Here&#8217;s a program that&#8217;s written in C, which adds up the numbers from 0 to 255 and prints out the result:<\/p>\n<pre># include <stdio.h>\r\n\r\nint main(void)\r\n{\r\n    int x, sum;\r\n    sum = 0;\r\n    x = 1;\r\n    while (x &lt; 256)\r\n    {\r\n        printf(\"%d \",x);\r\n        sum = sum + x;\r\n        x = x + 1;\r\n    }\r\n    printf(\"\\nSum = %d\\n\", sum);\r\n}<\/pre>\n<p>You may not understand everything in this program, but it certainly has a few English words in it, and even a programmer who doesn&#8217;t know C might be able to work their way through this program to identify how it works.<\/p>\n<p>We have a serious problem, however. You may have heard that computers don&#8217;t understand English: they only work in binary digits, or &#8220;bits,&#8221; the zeroes and ones that are represented by a billion switches being turned &#8220;off&#8221; or &#8220;on.&#8221;<\/p>\n<p><i>So how does the computer run this program?<\/i><\/p>\n<p>It doesn&#8217;t. But there is another program on the computer&#8211;a compiler&#8211;that is able to take this program and convert it to something called Assembly Language.<\/p>\n<h3>Assembly Language<\/h3>\n<p>In Apple&#8217;s macOS, if you have the Developer Tools installed, you can use the <b>gcc<\/b> compiler in the Terminal to output a compiled version of the program.<\/p>\n<p><code>$ gcc -o sum sum.c<\/code><\/p>\n<p>What does this new version of the program look like? We can see the Assembly Language version by using the <b>otool<\/b> program:<\/p>\n<p><code>$ otool -tv sum<br \/>\nsum:<br \/>\n(__TEXT,__text) section<br \/>\n_main:<br \/>\n0000000100000f10\tpushq\t%rbp<br \/>\n0000000100000f11\tmovq\t%rsp, %rbp<br \/>\n0000000100000f14\tsubq\t$0x20, %rsp<br \/>\n0000000100000f18\tmovl\t$0x0, -0x4(%rbp)<br \/>\n0000000100000f1f\tmovl\t$0x0, -0xc(%rbp)<br \/>\n0000000100000f26\tmovl\t$0x1, -0x8(%rbp)<br \/>\n0000000100000f2d\tcmpl\t$0x100, -0x8(%rbp)<br \/>\n0000000100000f34\tjge\t0x100000f65<br \/>\n0000000100000f3a\tleaq\t0x65(%rip), %rdi<br \/>\n0000000100000f41\tmovl\t-0x8(%rbp), %esi<br \/>\n0000000100000f44\tmovb\t$0x0, %al<br \/>\n0000000100000f46\tcallq\t0x100000f84<br \/>\n0000000100000f4b\tmovl\t-0xc(%rbp), %esi<br \/>\n0000000100000f4e\taddl\t-0x8(%rbp), %esi<br \/>\n0000000100000f51\tmovl\t%esi, -0xc(%rbp)<br \/>\n0000000100000f54\tmovl\t-0x8(%rbp), %esi<br \/>\n0000000100000f57\taddl\t$0x1, %esi<br \/>\n0000000100000f5a\tmovl\t%esi, -0x8(%rbp)<br \/>\n0000000100000f5d\tmovl\t%eax, -0x10(%rbp)<br \/>\n0000000100000f60\tjmp\t0x100000f2d<br \/>\n0000000100000f65\tleaq\t0x3e(%rip), %rdi<br \/>\n0000000100000f6c\tmovl\t-0xc(%rbp), %esi<br \/>\n0000000100000f6f\tmovb\t$0x0, %al<br \/>\n0000000100000f71\tcallq\t0x100000f84<br \/>\n0000000100000f76\tmovl\t-0x4(%rbp), %esi<br \/>\n0000000100000f79\tmovl\t%eax, -0x14(%rbp)<br \/>\n0000000100000f7c\tmovl\t%esi, %eax<br \/>\n0000000100000f7e\taddq\t$0x20, %rsp<br \/>\n0000000100000f82\tpopq\t%rbp<br \/>\n0000000100000f83\tretq<\/code><\/p>\n<p>This version of the program contains a series of commands&#8211;<code>push<\/code>, <code>move<\/code>, <code>jump<\/code>, <code>add<\/code>, <code>call<\/code>, <code>pop<\/code>, <code>return<\/code>&#8211;that manage the data in the program, addresses in memory (listed along the left side), and registers (like <code>%esi<\/code>). Some people program in assembly language, but you can see that it&#8217;s a much more complicated affair. The process of storing the value <code>1<\/code> in the variable <code>x<\/code> is, in assembly language<\/p>\n<p><code>movl\t$0x1, -0x8(%rbp)<\/code><\/p>\n<p>We can say that the assembly language instructions are &#8220;encapsulated,&#8221; or hidden away, so that we don&#8217;t have to worry about those implementation details. We can write our high-level code, and rest assured that the compilation process will take care of the dirty work for us.<\/p>\n<p>Of course, we still haven&#8217;t gotten down to the 0s and 1s that the computer needs to run a program. Let&#8217;s go one step farther down.<\/p>\n<h3>Machine Language<\/h3>\n<p>In the process of compiling, we actually created a binary version of the program, with nothing but 0s and 1s. We can use the <code>xxd<\/code> program in the terminal to view that code:<\/p>\n<p><code>$ xxd -b sum | cut -c 11-64<br \/>\n11001111 11111010 11101101 11111110 00000111 00000000<br \/>\n00000000 00000001 00000011 00000000 00000000 10000000<br \/>\n00000010 00000000 00000000 00000000 00001111 00000000<br \/>\n00000000 00000000 10110000 00000100 00000000 00000000<br \/>\n10000101 00000000 00100000 00000000 00000000 00000000<br \/>\n00000000 00000000 00011001 00000000 00000000 00000000<br \/>\n01001000 00000000 00000000 00000000 01011111 01011111<br \/>\n01010000 01000001 01000111 01000101 01011010 01000101<br \/>\n01010010 01001111 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000001 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00011001 00000000 00000000 00000000<br \/>\n11011000 00000001 00000000 00000000 01011111 01011111<br \/>\n01010100 01000101 01011000 01010100 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000001 00000000 00000000 00000000 00000000 00010000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 00000000 00010000 00000000 00000000<br \/>\n00000000 00000000 00000000 00000000 00000111 00000000<br \/>\n00000000 00000000 00000101 00000000 00000000 00000000<br \/>\n00000101 00000000 00000000 00000000 00000000 00000000<br \/>\n00000000 00000000 01011111 01011111 01110100 01100101<br \/>\n01111000 01110100 00000000 00000000 00000000 00000000<br \/>\n.<br \/>\n.<br \/>\n.<br \/>\n<\/code>  <\/p>\n<p>It is this code&#8211;these 1s and 0s&#8211;encapsulated several layers below our original version, that the computer uses to run the program.<\/p>\n<p>These 0s and 1s are used to operate the digital hardware, the memory locations and the logic gates, that produce a given output. In fact, before we had keyboards, mice, and monitors, the personal computer was simply a set of switches with lights above them. Computer code was entered using the individual switches&#8211;a long, painful, error-prone process&#8211;and output was read from the computer as a series of flashing lights.<\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=wdP8WB8Dwbg\">Here&#8217;s a programmer entering a program by hand onto such a machine<\/a>. The resulting program displays (in binary, of course!) the prime numbers between 2 and 255.<\/p>\n<p>Fortunately, we don&#8217;t have to enter binary logic instructions by hand. Those codes are encapsulated well beneath our high-level languages.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The idea of encapsulation is fundamental to computers in a number of ways. Generally speaking, &#8220;encapsulation&#8221; refers to the idea of building a container around something, as if that thing were contained in a capsule. When it comes to computers, there are a couple of slightly different ways the term might be used. Hiding code &hellip; <a href=\"https:\/\/www.hybridclassroom.com\/blog\/?p=1342\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Demo: Encapsulation<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[87,48,88,98,57],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1342"}],"collection":[{"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1342"}],"version-history":[{"count":8,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1342\/revisions"}],"predecessor-version":[{"id":1350,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1342\/revisions\/1350"}],"wp:attachment":[{"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hybridclassroom.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}