<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sshyamr</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sshyamr"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sshyamr"/>
	<updated>2026-08-11T02:47:45Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54247</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54247"/>
		<updated>2011-10-29T00:34:46Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Fixtures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails [http://guides.rubyonrails.org/getting_started.html#generating-a-model generate model] is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavioral-Driven-Development] goes hand in hand with [http://en.wikipedia.org/wiki/Test-driven_development Test-Driven-Development] and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;http://rspec.info/documentation/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54246</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54246"/>
		<updated>2011-10-29T00:32:27Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Setup prior to Testing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically ([http://en.wikipedia.org/wiki/Monkey_patch monkey]) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk). It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavioral-Driven-Development] goes hand in hand with [http://en.wikipedia.org/wiki/Test-driven_development Test-Driven-Development] and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;http://rspec.info/documentation/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54245</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54245"/>
		<updated>2011-10-29T00:30:37Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such [http://en.wikipedia.org/wiki/Behavior_Driven_Development Behavioral-Driven-Development] goes hand in hand with [http://en.wikipedia.org/wiki/Test-driven_development Test-Driven-Development] and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;http://rspec.info/documentation/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54244</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54244"/>
		<updated>2011-10-29T00:28:47Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara'''&amp;lt;ref&amp;gt;http://opinionated-programmer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/&amp;lt;/ref&amp;gt; is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;http://rspec.info/documentation/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54243</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54243"/>
		<updated>2011-10-29T00:27:49Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara''' is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2'''&amp;lt;ref&amp;gt;http://rspec.info/documentation/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54242</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54242"/>
		<updated>2011-10-29T00:26:46Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
The following example shows how the test framework '''CapyBara''' is used for Integration Testing:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
Another example of Integration Testing with Test-Framework '''RSpec Version 1.3.2''':&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
describe &amp;quot;Recipes&amp;quot; do&lt;br /&gt;
&lt;br /&gt;
  before(:all) do&lt;br /&gt;
    @recipe = Recipe.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  it &amp;quot;should not accept empty recipe&amp;quot; do&lt;br /&gt;
    @user.should_not_be_valid&lt;br /&gt;
    @user.title = &amp;quot;Cookie&amp;quot;&lt;br /&gt;
    @user.description = &amp;quot;Chocolate Chip Cookie&amp;quot;&lt;br /&gt;
    @user.instructions = &amp;quot;Bake in Oven&amp;quot;&lt;br /&gt;
    @user.category = 3&lt;br /&gt;
    @user.should_be_valid&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example we basically test that an empty recipe is invalid and that a recipe with fields filled out is valid. This is a very primitive example of using RSpec and it is just to showcase the difference between RSpec and Capybara, and is in no way a comprehensive example. A thing to note is the ''before(:all)'' method, which is similar to the setup() method in Java's JUnit Framework i.e. this method is called before every test in the ''describe'' block gets executed.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54152</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54152"/>
		<updated>2011-10-25T00:43:40Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, while running integration tests, one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54151</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54151"/>
		<updated>2011-10-25T00:39:53Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in rails: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, integration testing one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54122</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54122"/>
		<updated>2011-10-22T20:14:12Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in railsz: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, integration testing one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles&amp;lt;ref&amp;gt;http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html&amp;lt;/ref&amp;gt; have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54121</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=54121"/>
		<updated>2011-10-22T20:13:41Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in railsz: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, integration testing one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles[http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html] have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=53660</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=53660"/>
		<updated>2011-10-21T01:24:25Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ls]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c ap]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4a ga]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f sl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g as]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g nv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h kp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h lp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4j fw]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i lc]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=53653</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=53653"/>
		<updated>2011-10-21T01:22:46Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in railsz: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, integration testing one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles[http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html] have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most of the content for this article has been obtained from the Lecture taught in class which has been the primary primary resource. The video of the lecture can be found [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d here].&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=53646</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=53646"/>
		<updated>2011-10-21T01:19:20Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of [http://mediasite.eos.ncsu.edu/Mediasite/Viewer/?peid=786d700dbc65460695f7f0abf9e8cfa71d Lecture 10] '''&amp;quot;Testing in Rails&amp;quot;'''&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/testing.html&amp;lt;/ref&amp;gt; and it basically describes in detail the various types of tests in rails which one might encounter while developing a typical rails application. There are five components central to testing in railsz: '''Fixtures''', '''Unit tests''', '''Functional tests''', '''Integration tests''' and '''Performance tests'''. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem ''''memory_test_fix''''&amp;lt;ref&amp;gt;http://agilewebdevelopment.com/plugins/memory_test_fix&amp;lt;/ref&amp;gt; which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they would, if they were to read/write all their results to files (on the disk).It eliminates file locking issues on the test database when running on Windows. This is not a requirement, but it improves the speed of testing and development which is ultimately desirable. Most importantly it is good for testing because one usually does not need the data after the test is done, but only needs it during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the ''''config/database.yml'''' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the ''''database:'''' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one in memory and not in an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures&amp;lt;ref&amp;gt;http://ar.rubyonrails.org/classes/Fixtures.html&amp;lt;/ref&amp;gt; allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails '''generate model''' is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the '''&amp;quot;key: value&amp;quot;''' format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called '''':cookie'''' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating the model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that the ones which are generated by default by the scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is always a good practice to have one test for every kind of validation present so that everything that could potentially break has goes through  tests before production. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the ''''generate'''' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality by themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model ''''recipe'''' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Such a validation ensures that none of the fields can be empty and therefore creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what '''assert_false''' checks for. This test would pass if '''r.valid?''' is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if all the methods of a controller are working correctly. Since the controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, whether or not the users are getting authenticated correctly, validating the correctness of the content displayed on the page,etc. Functional tests are also used to test the Views because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionalities can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly or not.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a basic idea of what the test is all about.In this case, '''&amp;quot;should use layout&amp;quot;''' implies that this test checks the layout of the page. The gist of this test is that it gets the index page (corresponding to view of this controller),and  ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is '''&amp;quot;Online CookBook&amp;quot;'''. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example of it would be that of a shopping cart application. Even though different phases of the application may work correctly, integration testing one might realize that the ''''add to cart'''' button is absent in the product-catalog, even though the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such '''''Behavioral-Driven-Development''''' goes hand in hand with '''''Test-Driven-Development''''' and helps in removing the ambiguities which are often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedites the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must '''''require 'test_helper' '''''. This is basically a '''''mixin''''', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code&amp;lt;ref&amp;gt;http://guides.rubyonrails.org/performance_testing.html#modes&amp;lt;/ref&amp;gt;. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable and an inevitable part of development in rails.It must be fully exploited to avail the benefits associated with Test-Driven-Development, for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and many articles[http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html] have been written that emphasize this point.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52984</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52984"/>
		<updated>2011-10-20T07:47:12Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one might encounter while developing a typical rails application. There are four components central to testing in rails: Fixtures, Unit tests, Functional tests, Integration tests and Performance tests. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable an inevitable part of development in rails and must be fully exploited to avail the benefits associated with Test-Driven-Development, if for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and [http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html entire articles] have been written that emphasize this point.&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52983</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52983"/>
		<updated>2011-10-20T07:46:02Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one might encounter while developing a typical rails application. There are four components central to testing in rails: Fixtures, Unit tests, Functional tests, Integration tests and Performance tests. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit in addition to all the methods offered by Capybara.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable an inevitable part of development in rails and must be fully exploited to avail the benefits associated with Test-Driven-Development, if for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and [http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html entire articles] have been written that emphasize this point.&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52982</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52982"/>
		<updated>2011-10-20T07:45:20Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one might encounter while developing a typical rails application. There are four components central to testing in rails: Fixtures, Unit tests, Functional tests, Integration tests and Performance tests. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration test written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable an inevitable part of development in rails and must be fully exploited to avail the benefits associated with Test-Driven-Development, if for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and [http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html entire articles] have been written that emphasize this point.&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52981</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52981"/>
		<updated>2011-10-20T07:40:20Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one might encounter while developing a typical rails application. There are four components central to testing in rails: Fixtures, Unit tests, Functional tests, Integration tests and Performance tests. These have been described below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable an inevitable part of development in rails and must be fully exploited to avail the benefits associated with Test-Driven-Development, if for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and [http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html entire articles] have been written that emphasize this point.&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52980</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52980"/>
		<updated>2011-10-20T07:38:55Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one might encounter while developing a typical rails application. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Testing is an indispensable an inevitable part of development in rails and must be fully exploited to avail the benefits associated with Test-Driven-Development, if for the simple reason that rails provides an excellent in-built framework upon which writing tests is a highly natural and intuitive process. There are many advantages to testing and [http://www.learn.geekinterview.com/programming/ruby/ruby-on-rails-application-testing.html entire articles] have been written that emphasize this point.&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52979</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52979"/>
		<updated>2011-10-20T07:30:40Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Performance Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one might encounter while developing a typical rails application. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
Performance tests as the name indicates are used to gauge the performance of the system and play a very important role in software development for the simple reason that as a developer, one does not want the end user to have a poor experience while using the application. Users do not want to wait long for pages to load and elements on the page to respond. They are not - and should not - be concerned with the capability of the system to handle large loads, scale to accommodate increased volumes of traffic etc. Such details are abstracted away from the user, but they ''do'' have a significant impact on user's interaction with the system.&lt;br /&gt;
&lt;br /&gt;
Rails Performance test can be categorized as a special type of integration tests, which are designed for bench-marking and profiling the test code. In these tests, one can mention how many connections are to be simulated to the server etc. at the outcome of which it would be possible to identify the performance bottlenecks and hopefully pinpoint the source of speed and/or memory problems.&lt;br /&gt;
&lt;br /&gt;
Detailed examples can be found [http://guides.rubyonrails.org/performance_testing.html#examples Here].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52978</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52978"/>
		<updated>2011-10-20T07:09:41Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one might encounter while developing a typical rails application. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52977</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52977"/>
		<updated>2011-10-20T07:08:53Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Integration Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one encounters while developing a typical rails app. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
  visit categories_path&lt;br /&gt;
  click_link “New category”&lt;br /&gt;
  fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
  click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
  include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52976</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52976"/>
		<updated>2011-10-20T07:08:25Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Functional Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one encounters while developing a typical rails app. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
  get :index&lt;br /&gt;
  assert_response :success&lt;br /&gt;
  assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
	visit categories_path&lt;br /&gt;
	click_link “New category”&lt;br /&gt;
	fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
	click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
	include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52975</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52975"/>
		<updated>2011-10-20T07:07:55Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Fixtures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one encounters while developing a typical rails app. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
	get :index&lt;br /&gt;
	assert_response :success&lt;br /&gt;
	assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
	visit categories_path&lt;br /&gt;
	click_link “New category”&lt;br /&gt;
	fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
	click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
	include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52974</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52974"/>
		<updated>2011-10-20T07:07:01Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Unit Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one encounters while developing a typical rails app. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;%end%&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder. Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
	get :index&lt;br /&gt;
	assert_response :success&lt;br /&gt;
	assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
	visit categories_path&lt;br /&gt;
	click_link “New category”&lt;br /&gt;
	fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
	click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
	include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52973</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52973"/>
		<updated>2011-10-20T07:06:30Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one encounters while developing a typical rails app. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;%end%&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
Unit tests are used to test the models. Thus any tests that deal with the validation of data in a model are to be done in Unit tests. It is good practice to have one test for every kind of validation present so the everything that could potentially break has been tested. Models contain most of the business logic, hence the unit tests need to test all the individual methods present in the models too.&lt;br /&gt;
&lt;br /&gt;
When models are generated (either using the 'generate' command or by using scaffolds), default Unit test are generated alongside. These tests by no means contain any functionality in and of themselves, but are merely placeholders that provide a framework upon which one can write their own tests. Such test stubs are created in the test/unit folder.&lt;br /&gt;
&lt;br /&gt;
Almost all tests, be it Unit tests or Integration tests require 'test_helper' which specifies the default configuration of our tests. &lt;br /&gt;
&lt;br /&gt;
Consider the CookBook example in which the model 'recipe' validated the presence of its attributes 'title', 'description', 'instructions' and 'category'. Since this validation ensures that none of the fields can be empty, creation of an empty recipe should be invalid. A Unit test that does just this is given below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test 'should require all fields' do&lt;br /&gt;
  r = recipe.new&lt;br /&gt;
  assert_false r.valid?&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A blank recipe should be invalidated by the model which is what assert_false checks for. This test would pass if r.valid? is false i.e. model does not accept an empty recipe.&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
	get :index&lt;br /&gt;
	assert_response :success&lt;br /&gt;
	assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
	visit categories_path&lt;br /&gt;
	click_link “New category”&lt;br /&gt;
	fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
	click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
	include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52964</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52964"/>
		<updated>2011-10-20T06:22:47Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one encounters while developing a typical rails app. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Setup prior to Testing==&lt;br /&gt;
&amp;lt;h4&amp;gt;In-Memory Databases:&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since all tests involve a high amount of database interaction, it is highly recommended to install the gem 'memory_test_fix' which basically (monkey) patches all tests in rails. This gem allows your tests to mock up a database within the memory, so that all reads/writes to the database executed by the test (when they run) are done to memory instead of the disk. This helps run all the unit tests a lot faster than what they otherwise would if they were to read/write all their results to files (on the disk) and also because it will eliminate file locking issues on the test database when running on Windows. This is in no way a requirement, but it improves the speed of testing and development which is ultimately desirable. This is especially good for testing because one usually does not need the data after the test is done, but only during the lifetime of the test.&lt;br /&gt;
&lt;br /&gt;
Make the following change to the 'config/database.yml' file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test:&lt;br /&gt;
  adapter: sqlite3&lt;br /&gt;
  database: &amp;quot;:memory:&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The change is that the 'database:' field has been changed from:&lt;br /&gt;
&amp;lt;pre&amp;gt;db/development.sqlite3 to &amp;quot;:memory:&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This now ensures that for all the tests, the database used will be the one on memory and not an actual Sqlite database.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
Rails tests are data-driven, which means that all of its tests need some sort of sample data to run on. Fixtures allow the tester to populate the testing database before any of the tests in the test folder can run. Fixtures have a file format which describes data structures in a human readable format and can be found under the 'test/fixtures' directory. When the rails generate model is executed to create a new model, fixture stubs are automatically created and placed in that directory. YAML fixtures are stored in a single file per model i.e. for every model there is a corresponding fixture. Each record is given a name and is followed by an indented list of key/value pairs in the &amp;quot;key: value&amp;quot; format. When you create a fixture, it generates an internal hash table. Fixtures are hash objects which  can be accessed directly because it is automatically setup as a local variable for the test case. The good thing about this is that we can reference these objects using symbolic names. So if we were to declare a fixture called ':cookie' (see example below), we could reference the entire cookie record simply by:&lt;br /&gt;
&amp;lt;pre&amp;gt;categories(:cookie)&amp;lt;/pre&amp;gt;&lt;br /&gt;
This will return the hash for the fixture named cookie which corresponds to a row in the recipe table describing the recipe for that cookie.&lt;br /&gt;
&lt;br /&gt;
On creating model, the default fixtures generated are of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
  &lt;br /&gt;
two:&lt;br /&gt;
  title: MyString&lt;br /&gt;
  description: MyString&lt;br /&gt;
  instructions: MyText&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We spoke of the :cookie fixture which would be defined as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cookie:&lt;br /&gt;
  Title: Biscuit&lt;br /&gt;
  Description: Round and Small &lt;br /&gt;
  Instructions: Buy and bake them &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This allows us to access this entire record using the symbolic name ':cookie' which hashes to this particular fixture.&lt;br /&gt;
&lt;br /&gt;
An important feature of YAML fixtures is that it supports Embedded Ruby i.e. we can embed ruby code into fixtures to generate a large set of sample data. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;% (1..1000).each do |i| %&amp;gt;&lt;br /&gt;
fix_&amp;lt;%= i %&amp;gt;:&lt;br /&gt;
  name: category_&amp;lt;%= i %&amp;gt;&lt;br /&gt;
&amp;lt;%end%&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This would create a thousand fixtures having symbolic names fix_1, fix_2 up to fix_1000, each one of them having a corresponding name attribute category_1, category_2 etc. This is a much better alternative than having to copy-paste the fixture fixture a thousand times.&lt;br /&gt;
&lt;br /&gt;
A very important thing to remember about fixtures is that ones default-generated by scaffolds  do not factor in for any foreign-key relationships that might be present in the models. Thus, such references have to be explicitly added to the fixture manually in order to reflect any 'has-many' or 'belongs-to' relationships across models.&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
	get :index&lt;br /&gt;
	assert_response :success&lt;br /&gt;
	assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
	visit categories_path&lt;br /&gt;
	click_link “New category”&lt;br /&gt;
	fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
	click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
	include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52953</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52953"/>
		<updated>2011-10-20T05:44:36Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
==Introduction==&lt;br /&gt;
This article is a summary of Lecture 10 &amp;quot;Testing in Rails&amp;quot; and it basically describes in detail the various types of tests in rails one encounters while developing a typical rails app. There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests. These have been describes below.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Unit Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Functional Tests==&lt;br /&gt;
Fundamentally, these tests are used for testing the 'functionality' of the various components of a controller. So typically, one has as many functional tests as there are controllers. The basic purpose of writing functional tests is to check if methods of a controller are working correctly. Since controllers often influence the content of the web page (which is rendered by the corresponding method of a controller) functional tests are typically written to check if the controller’s method is rendering/redirecting to the correct page, that users are getting authenticated correctly, that the content displayed on the page is correct etc. Functional tests are also used to test the View because controllers and views are tightly coupled in rails. This tight coupling is evident by the fact that instance variables in the controller are available to the view. Hence, view related functionality can also be tested in functional tests e.g. the most common kind of view test would be checking that the title of a web page is  displayed correctly.&lt;br /&gt;
&lt;br /&gt;
An example of a functional test:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “should use layout” do&lt;br /&gt;
	get :index&lt;br /&gt;
	assert_response :success&lt;br /&gt;
	assert_select ‘title’, ‘Online Cookbook’&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In rails the title of the test gives you a absic idea of what the test does. In this case,  &amp;quot;should use layout&amp;quot; implies that this test checks the layout of the page. The gist of this test is that it GET's the index page (corresponding to view of this controller), ensures that the page is rendered correctly with the mothod: &amp;lt;pre&amp;gt; assert_response &amp;lt;/pre&amp;gt;Finally it checks that the title of the page is &amp;quot;Online CookBook&amp;quot;. &amp;lt;pre&amp;gt; assert_select &amp;lt;/pre&amp;gt; This method allows you to select the value of a particular tag from html - in this case the title tag.&lt;br /&gt;
&lt;br /&gt;
==Integration Tests==&lt;br /&gt;
Typically in software development, different modules of a project are worked on by different teams/developers. Each team might ensure that the model works correctly in-itself, but this might not necessarily be the case when all the modules are coupled together as a single unit. This is where Integration tests come into play. They test the interaction between multiple controllers and all the components in a sequence, end-to-end. An example would be that of a shopping cart application. Even though different phases of the application may work correctly while integration testing one might realize that the 'add to cart' button is absent in the product-catalog although the add to cart functionality has been correctly implemented.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default integration tests framework included in Test-Unit are very low level i.e. they deal with HTTP GET, POST requests responses, session objects, cookies, redirects etc. In ''Behavioral-Driven-Development'' we want to deal with the system on a higher level – similar to a user’s interaction with the system i.e. we want to deal only with clicks, with typing etc. Hence, we can use some of the popular Integration Testing frameworks like Capybara which is a GUI testing framework and allows one to specify - within a test - various actions like 'click' to click on a button, 'fill_in' to fill some text into a designated text-box etc. We can see that this is at a high level and somewhat analogous to actions an end-user might go through while using the application. So the rule of thumb while writing integration tests is to identify the end-users requirements and scope of interaction with the system, walk through the steps that they would take and mimic those in the form of tests. It is clearly evident how such ''Behavioral-Driven-Development'' goes hand in hand with ''Test-Driven-Development'' and helps in removing the ambiguities often associated with Customer Requirements.&lt;br /&gt;
&lt;br /&gt;
An Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
test “create category from main page” do&lt;br /&gt;
	visit categories_path&lt;br /&gt;
	click_link “New category”&lt;br /&gt;
	fill_in “category_name”, :with =&amp;gt; “Sample Category”&lt;br /&gt;
	click_button “Create Category”&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here we have simulated a user-action (for the CookBook example) where the user would carry out the following steps:&lt;br /&gt;
# Visit the Categories Home Page (whose url is specified as categories_path by the routes.rb file)&lt;br /&gt;
# Click on the Link which says &amp;quot;New Category&amp;quot;, which would lead to another page.&lt;br /&gt;
# On this new page, fill the text-box with some text, say &amp;quot;Sample Category&amp;quot;&lt;br /&gt;
# Click on the button that says  &amp;quot;Create Category&amp;quot;.&lt;br /&gt;
One can easily identify these actions from the code which is highly intuitive and self-explanatory. Capybara thus provides us with these convenient methods which greatly expedite the whole Integration Testing process.&lt;br /&gt;
&lt;br /&gt;
To use the framework, simply include the corresponding gem in the Gemfile, and the following lines to the end of the ''test_helper.rb'' file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Add more helper methods ...&lt;br /&gt;
require ‘capybara/rails’&lt;br /&gt;
&lt;br /&gt;
class ActionDispatch::IntegrationTest&lt;br /&gt;
	include Capybara::DSL&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The easy-to-use commands mentioned before are created by Capybara using a Domain Specific Language (DSL) and in order to be able to use it, every Integration written must ''require 'test_helper' ''. This is basically a ''mixin'', so one still has the capability to access all the low-level GET/POST commands in Test-Unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In sum, Integration tests are vital and are carried out in the final stages of testing to ensure that the system works as a cohesive and complete unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Performance Tests==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52487</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52487"/>
		<updated>2011-10-18T19:41:48Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Lecture 10 - Testing in Rails&amp;lt;/h2&amp;gt;&lt;br /&gt;
__TOC__&lt;br /&gt;
== Introduction ==&lt;br /&gt;
This article covers testing in rails which is the content of Lecture 10.&lt;br /&gt;
There are four components central to testing in rails: Fixtures, Unit tests, Functional Tests, Integration Tests and Performance tests.&lt;br /&gt;
&lt;br /&gt;
==Fixtures==&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52470</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52470"/>
		<updated>2011-10-18T19:19:16Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ls]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c ap]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4a ga]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f sl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f ss]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52468</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=52468"/>
		<updated>2011-10-18T19:18:31Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ls]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c ap]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4a ga]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f sl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f ss]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52467</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4e gs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4e_gs&amp;diff=52467"/>
		<updated>2011-10-18T19:17:15Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: Created page with &amp;quot;PlaceHolder&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;PlaceHolder&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51605</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51605"/>
		<updated>2011-09-30T23:11:44Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Ruby : The send method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java &amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C# &amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methods for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for accountNumber allows public acccess to it.&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	//setter methods for routingNumber allows public acccess to it.	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for balance allows only 'protected' access to it (see definition).&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows accountNumber to be read publicly	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows routingNumber to be read publicly		&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	// Getter method allows balance to be read only in in a 'protected' fashion (see definition).&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
  #Instance Variables&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  #Initialize the instance variables&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
      @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
  #method to compare protected data member, balance of 2 objects&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
  #access_specifier:class_member_name to define the access control&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Creating objects and initializing variables&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
&lt;br /&gt;
#Accessing public data member&lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
&lt;br /&gt;
#Comparing protected field: balance of Steve and Anna&lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
&lt;br /&gt;
#Unsuccessful access of private data member raises “NoMethodError” &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
&lt;br /&gt;
The ‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
====Ruby : The send method====&lt;br /&gt;
The [http://ruby-doc.org/core/classes/Object.html#M000999 send method] of Ruby has always been a potential security leak and has thus been continually revised and changed across the different versions of Ruby. The Ruby send method has been de-mystified to an extent below.&lt;br /&gt;
&lt;br /&gt;
In Ruby calling methods is similar to passing messages to an object. Consider the simple code below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class TestSend&lt;br /&gt;
    def hello&lt;br /&gt;
	puts “hello world”&lt;br /&gt;
    end&lt;br /&gt;
    public:hello&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the hello method of TestSend can be invoked in 2 ways as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = TestSend.new&lt;br /&gt;
&lt;br /&gt;
#Conventional invocation of public methods&lt;br /&gt;
t.hello&lt;br /&gt;
&lt;br /&gt;
#Invoking a method using send&lt;br /&gt;
t.send :hello&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Both these ways of calling the hello method prints out &amp;quot;hello world&amp;quot; as expected. Parameters can also be passed while invoking a method using the send method. However in Ruby, using send we can also access the private methods of a class. Consider the following snippet for an illustration. Here we have slightly modified the TestSend class we had defined above.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class TestSend&lt;br /&gt;
    def hello&lt;br /&gt;
	puts “hello world”&lt;br /&gt;
    end&lt;br /&gt;
    private:hello&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The hello method has been changed to private. Lets again consider 2 ways of invoking this private method hello:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t=TestSend.new&lt;br /&gt;
&lt;br /&gt;
#Conventional invocation of public methods&lt;br /&gt;
t.hello&lt;br /&gt;
&lt;br /&gt;
#Invoking a method using send&lt;br /&gt;
t.send :hello&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As expected 't.hello'  results in an 'NoMethodError' whereas 't.send :hello' successfully prints &amp;quot;hello world&amp;quot;. Thus, the send method provides a workaround to accessing private member functions of a class. This is a distinction that Ruby possesses in comparison to other O-O languages. The state of send has changed several times as and when versions of Ruby have been released. For a while, Ruby 1.9 changed the implementation of send to only allow public methods to be called, introducing a new 'send!' method which continued to allow private methods to be called. However, this has now been removed from Ruby 1.9 in favour of keeping the functionality of send as is. Instead, a new public_send method has been introduced which will only call public methods &amp;lt;ref&amp;gt;http://deaddeadgood.com/2008/11/17/rubys-send-method/&amp;lt;/ref&amp;gt;. Another [http://www.joshstaiger.org/archives/2006/12/the_ruby_send_h.html example].&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the principal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51604</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51604"/>
		<updated>2011-09-30T23:10:59Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Ruby : The send method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java &amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C# &amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methods for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for accountNumber allows public acccess to it.&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	//setter methods for routingNumber allows public acccess to it.	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for balance allows only 'protected' access to it (see definition).&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows accountNumber to be read publicly	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows routingNumber to be read publicly		&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	// Getter method allows balance to be read only in in a 'protected' fashion (see definition).&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
  #Instance Variables&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  #Initialize the instance variables&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
      @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
  #method to compare protected data member, balance of 2 objects&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
  #access_specifier:class_member_name to define the access control&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Creating objects and initializing variables&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
&lt;br /&gt;
#Accessing public data member&lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
&lt;br /&gt;
#Comparing protected field: balance of Steve and Anna&lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
&lt;br /&gt;
#Unsuccessful access of private data member raises “NoMethodError” &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
&lt;br /&gt;
The ‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
====Ruby : The send method====&lt;br /&gt;
The [http://ruby-doc.org/core/classes/Object.html#M000999 send method] of Ruby has always posed a potential security leak and has thus been continually revised and changed across the different versions of Ruby. The Ruby send method has been de-mystified to an extent below.&lt;br /&gt;
&lt;br /&gt;
In Ruby calling methods is similar to passing messages to an object. Consider the simple code below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class TestSend&lt;br /&gt;
    def hello&lt;br /&gt;
	puts “hello world”&lt;br /&gt;
    end&lt;br /&gt;
    public:hello&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the hello method of TestSend can be invoked in 2 ways as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = TestSend.new&lt;br /&gt;
&lt;br /&gt;
#Conventional invocation of public methods&lt;br /&gt;
t.hello&lt;br /&gt;
&lt;br /&gt;
#Invoking a method using send&lt;br /&gt;
t.send :hello&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Both these ways of calling the hello method prints out &amp;quot;hello world&amp;quot; as expected. Parameters can also be passed while invoking a method using the send method. However in Ruby, using send we can also access the private methods of a class. Consider the following snippet for an illustration. Here we have slightly modified the TestSend class we had defined above.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class TestSend&lt;br /&gt;
    def hello&lt;br /&gt;
	puts “hello world”&lt;br /&gt;
    end&lt;br /&gt;
    private:hello&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The hello method has been changed to private. Lets again consider 2 ways of invoking this private method hello:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t=TestSend.new&lt;br /&gt;
&lt;br /&gt;
#Conventional invocation of public methods&lt;br /&gt;
t.hello&lt;br /&gt;
&lt;br /&gt;
#Invoking a method using send&lt;br /&gt;
t.send :hello&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As expected 't.hello'  results in an 'NoMethodError' whereas 't.send :hello' successfully prints &amp;quot;hello world&amp;quot;. Thus, the send method provides a workaround to accessing private member functions of a class. This is a distinction that Ruby possesses in comparison to other O-O languages. The state of send has changed several times as and when versions of Ruby have been released. For a while, Ruby 1.9 changed the implementation of send to only allow public methods to be called, introducing a new 'send!' method which continued to allow private methods to be called. However, this has now been removed from Ruby 1.9 in favour of keeping the functionality of send as is. Instead, a new public_send method has been introduced which will only call public methods &amp;lt;ref&amp;gt;http://deaddeadgood.com/2008/11/17/rubys-send-method/&amp;lt;/ref&amp;gt;. Another [http://www.joshstaiger.org/archives/2006/12/the_ruby_send_h.html example].&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the principal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51603</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51603"/>
		<updated>2011-09-30T23:07:28Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java &amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C# &amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methods for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for accountNumber allows public acccess to it.&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	//setter methods for routingNumber allows public acccess to it.	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for balance allows only 'protected' access to it (see definition).&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows accountNumber to be read publicly	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows routingNumber to be read publicly		&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	// Getter method allows balance to be read only in in a 'protected' fashion (see definition).&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
  #Instance Variables&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  #Initialize the instance variables&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
      @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
  #method to compare protected data member, balance of 2 objects&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
  #access_specifier:class_member_name to define the access control&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Creating objects and initializing variables&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
&lt;br /&gt;
#Accessing public data member&lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
&lt;br /&gt;
#Comparing protected field: balance of Steve and Anna&lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
&lt;br /&gt;
#Unsuccessful access of private data member raises “NoMethodError” &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
&lt;br /&gt;
The ‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
====Ruby : The send method====&lt;br /&gt;
The send method of Ruby has always posed a potential security leak and has thus been continually revised and changed across the different versions of Ruby. The Ruby send method has been de-mystified to an extent below.&lt;br /&gt;
&lt;br /&gt;
In Ruby calling methods is similar to passing messages to an object. Consider the simple code below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class TestSend&lt;br /&gt;
    def hello&lt;br /&gt;
	puts “hello world”&lt;br /&gt;
    end&lt;br /&gt;
    public:hello&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now the hello method of TestSend can be invoked in 2 ways as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t = TestSend.new&lt;br /&gt;
&lt;br /&gt;
#Conventional invocation of public methods&lt;br /&gt;
t.hello&lt;br /&gt;
&lt;br /&gt;
#Invoking a method using send&lt;br /&gt;
t.send :hello&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Both these ways of calling the hello method prints out &amp;quot;hello world&amp;quot; as expected. Parameters can also be passed while invoking a method using the send method. However in Ruby, using send we can also access the private methods of a class. Consider the following snippet for an illustration. Here we have slightly modified the TestSend class we had defined above.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class TestSend&lt;br /&gt;
    def hello&lt;br /&gt;
	puts “hello world”&lt;br /&gt;
    end&lt;br /&gt;
    private:hello&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The hello method has been changed to private. Lets again consider 2 ways of invoking this private method hello:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
t=TestSend.new&lt;br /&gt;
&lt;br /&gt;
#Conventional invocation of public methods&lt;br /&gt;
t.hello&lt;br /&gt;
&lt;br /&gt;
#Invoking a method using send&lt;br /&gt;
t.send :hello&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As expected 't.hello'  results in an 'NoMethodError' whereas 't.send :hello' successfully prints &amp;quot;hello world&amp;quot;. Thus, the send method provides a workaround to accessing private member functions of a class. This is a distinction that Ruby possesses in comparison to other O-O languages. The state of send has changed several times as and when versions of Ruby have been released. For a while, Ruby 1.9 changed the implementation of send to only allow public methods to be called, introducing a new 'send!' method which continued to allow private methods to be called. However, this has now been removed from Ruby 1.9 in favour of keeping the functionality of send as is. Instead, a new public_send method has been introduced which will only call public methods &amp;lt;ref&amp;gt;http://deaddeadgood.com/2008/11/17/rubys-send-method/&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the principal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51602</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51602"/>
		<updated>2011-09-30T22:55:00Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java &amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C# &amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methods for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for accountNumber allows public acccess to it.&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	//setter methods for routingNumber allows public acccess to it.	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for balance allows only 'protected' access to it (see definition).&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows accountNumber to be read publicly	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows routingNumber to be read publicly		&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	// Getter method allows balance to be read only in in a 'protected' fashion (see definition).&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
  #Instance Variables&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
  #Initialize the instance variables&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
      @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
  #method to compare protected data member, balance of 2 objects&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
  #access_specifier:class_member_name to define the access control&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Creating objects and initializing variables&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
&lt;br /&gt;
#Accessing public data member&lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
&lt;br /&gt;
#Comparing protected field: balance of Steve and Anna&lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
&lt;br /&gt;
#Unsuccessful access of private data member raises “NoMethodError” &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the principal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51600</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51600"/>
		<updated>2011-09-30T22:51:49Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java &amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C# &amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methods for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for accountNumber allows public acccess to it.&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	//setter methods for routingNumber allows public acccess to it.	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	//setter methods for balance allows only 'protected' access to it (see definition).&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows accountNumber to be read publicly	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Getter method allows routingNumber to be read publicly		&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	// Getter method allows balance to be read only in in a 'protected' fashion (see definition).&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the principal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51596</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51596"/>
		<updated>2011-09-30T22:45:46Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java &amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C# &amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methods for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the principal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51595</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51595"/>
		<updated>2011-09-30T22:45:05Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Examples of Access Specifiers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java &amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C# &amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methods for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51593</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51593"/>
		<updated>2011-09-30T22:43:19Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Evolution of Access Control and proliferation of Accessor methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system &amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java &amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C# &amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51554</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51554"/>
		<updated>2011-09-30T17:00:36Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Evolution of Access Control and proliferation of Accessor methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state, had to be sent as a request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not being explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51553</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51553"/>
		<updated>2011-09-30T16:59:49Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Evolution of Access Control and proliferation of Accessor methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many O-O languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51552</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51552"/>
		<updated>2011-09-30T16:59:00Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some of the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51551</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51551"/>
		<updated>2011-09-30T16:58:18Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* C# */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class. Also see [http://www.informit.com/articles/article.aspx?p=101373&amp;amp;seqNum=3 this] for another example.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51550</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51550"/>
		<updated>2011-09-30T16:53:31Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* C# */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private   string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
&lt;br /&gt;
        // accountNumber is private, so it can only be modified from within this &lt;br /&gt;
        // class. Also, since the following method is private, it can only be called&lt;br /&gt;
        // from within this class. So this field is truly private.&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
        // routingNumber is protected, so it can be directly modified by objects of&lt;br /&gt;
        // a subclass. But the contents of this field are made accessible as public&lt;br /&gt;
        // so anyone can 'read' the value of this field.&lt;br /&gt;
	public string getRoutingNumber()&lt;br /&gt;
	{&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	// Provides access to modify the private member balance, but this function&lt;br /&gt;
	// can only be called by subclasses (and from within this class).&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
        // Provides access to 'read' the contents of the private field to everyone.&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51549</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51549"/>
		<updated>2011-09-30T16:42:10Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data members routingNumber, accountNumber are made private and balance is protected. This makes it impossible to directly access or change routingNumber, accountNumber from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). However, private and protected members are accessible through friend functions, so displayAccountInfo() function is able to access all the mentioned fields. Since balance is protected, it can be accessed from a subclass (CheckingAccount) directly as shown above. But the private members have to be accessed through the functions changeRoutingNumber() and changeAccountNumber() instead of being accessed directly.&lt;br /&gt;
&lt;br /&gt;
Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
	&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private void setRoutingNumber(string rtNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51548</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51548"/>
		<updated>2011-09-30T16:36:12Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
&lt;br /&gt;
	protected:&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingtNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	friend void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *accNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingtNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Friend function can access both the private and protected members &lt;br /&gt;
// of BankAccount.&lt;br /&gt;
void displayAccountInfo(BankAccount ba) {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number: ” &amp;lt;&amp;lt; ba.accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number: ” &amp;lt;&amp;lt; ba.routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance: ” &amp;lt;&amp;lt; ba.balance &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// CheckingAccount inherits from BankAccount. Hence it can access only the&lt;br /&gt;
// public and protected members of BankAccount. It cannot access the private&lt;br /&gt;
// members of BankAccount.&lt;br /&gt;
class CheckingAccount: public BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char accountType[20];&lt;br /&gt;
&lt;br /&gt;
	public:&lt;br /&gt;
	CheckingAccount(char *, char *);&lt;br /&gt;
	void resetBalance();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
CheckingAccount::CheckingAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(accountType, &amp;quot;Checking&amp;quot;);&lt;br /&gt;
	changeRoutingtNumber(rNo);&lt;br /&gt;
	changeAccountNumber (accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Since balance is a protected field, it can be accessed by CheckingAccount&lt;br /&gt;
CheckingAccount::void resetBalance() {&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
	CheckingAccount ca(&amp;quot;888&amp;quot;, &amp;quot;999&amp;quot;);&lt;br /&gt;
	ca.depositMoney(100);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.withdrawMoney(50);&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
	ca.resetBalance();	&lt;br /&gt;
	displayAccountInfo(ca);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
OUTPUT:&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 100&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 50&lt;br /&gt;
&lt;br /&gt;
Account Number: 888&lt;br /&gt;
Routing Number: 999&lt;br /&gt;
Account Balance: 0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the datamambers routingNumber, accountNumber and balance are made private. This makes it impossible to directly access  or change these data members from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
	&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private void setRoutingNumber(string rtNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51547</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51547"/>
		<updated>2011-09-30T16:08:33Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined [http://en.wikipedia.org/wiki/Dynamic_programming_language dynamically], as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingtNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *acNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingtNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, arNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::displayAccountInfo() {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number:” &amp;lt;&amp;lt; accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number:” &amp;lt;&amp;lt; routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance:” &amp;lt;&amp;lt; balance &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the datamambers routingNumber, accountNumber and balance are made private. This makes it impossible to directly access  or change these data members from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
	&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private void setRoutingNumber(string rtNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51546</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51546"/>
		<updated>2011-09-30T16:06:17Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same [http://download.oracle.com/javase/tutorial/java/concepts/package.html Package]. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them &amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/java/concepts/package.html&amp;lt;/ref&amp;gt;).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingtNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *acNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingtNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, arNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::displayAccountInfo() {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number:” &amp;lt;&amp;lt; accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number:” &amp;lt;&amp;lt; routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance:” &amp;lt;&amp;lt; balance &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the datamambers routingNumber, accountNumber and balance are made private. This makes it impossible to directly access  or change these data members from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
	&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private void setRoutingNumber(string rtNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51545</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51545"/>
		<updated>2011-09-30T16:02:24Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* C# */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same package. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the [http://msdn.microsoft.com/en-us/library/ms173099.aspx Assembly] (also see [http://msdn.microsoft.com/en-us/library/ms173099(v=vs.80).aspx this])  in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingtNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *acNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingtNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, arNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::displayAccountInfo() {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number:” &amp;lt;&amp;lt; accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number:” &amp;lt;&amp;lt; routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance:” &amp;lt;&amp;lt; balance &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the datamambers routingNumber, accountNumber and balance are made private. This makes it impossible to directly access  or change these data members from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
	&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private void setRoutingNumber(string rtNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51544</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2b ns</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2b_ns&amp;diff=51544"/>
		<updated>2011-09-30T15:58:59Z</updated>

		<summary type="html">&lt;p&gt;Sshyamr: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;h2&amp;gt;Access Control in Object-Oriented Languages&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A fundamental precept of O-O systems is that an object should not expose any of its implementation details. In other words, access to an object’s functionality must be tightly regulated. The access control policies of any object-oriented language specify the rules and capabilities for enforcing this regulation. Thus, the concept of Access control ties in deeply with the underlying principles of [http://en.wikipedia.org/wiki/Object-oriented_programming Object Oriented Programming].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
==Purpose of Access Control==&lt;br /&gt;
Object-oriented programming fundamentally revolves around the principle of Data Hiding and Encapsulation. In a nutshell, [http://en.wikipedia.org/wiki/Information_hiding Data Hiding] is the practice of securing data by constraining access to it, and [http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) Encapsulation] ensures that the secure data (and the respective member functions) are bundled together as an individual module. All object oriented languages thus need to control the level of access to their critical data (and functions) which is achieved using ''Access Modifiers'' or ''Access Specifiers'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The need to strictly control ''who'' can access ''what'' is very crucial in Software development. For example, if you make an instance variable 'public', then you can't change the field as the class evolves over time since you might break any external code that uses the field. This is because outside code having complete access to this field, has tied itself indelibly to the inside implementation of the (former) class and is now closely dependent on it. Changing the said field thus entails changing all the other code that have a dependency on it, which is cumbersome, unwieldy and contrary to the principles of an Object Oriented approach which strives for [http://en.wikipedia.org/wiki/Coupling_(computer_programming) Low Coupling].&lt;br /&gt;
&lt;br /&gt;
Thus, the ‘Implementation hiding principle’ leads to a good acid test of an O-O system's quality: Can you make massive changes to a class definition—even throw out the whole thing and replace it with a completely different implementation—without impacting any of the code that uses that class's objects? This sort of modularization is the central premise of object orientation and makes maintenance much easier. Without implementation hiding, there's little point in using other O-O features. Regulation of access - Access Control - is at the heart of Object oriented programming&amp;lt;ref&amp;gt;http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Evolution of Access Control and proliferation of Accessor methods==&lt;br /&gt;
Access control has been a part of the OOP movement ever since its inception. [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] is arguably one of first and purest of the many OO languages and was developed by Xerox PARC  in the Learning Research Group in the 1970s &amp;lt;ref&amp;gt;http://www.zdnet.com/blog/murphy/the-tattered-history-of-oop/1157&amp;lt;/ref&amp;gt;. Rather than having explicit access specifiers, smalltalk objects operated more in the sense of 'states' i.e. it would hold the 'state' of object(s) which is always private to that object. Any changes required to be made to this state had to be sent as request message to that object. This is the most primitive form of access control - having private data members and public member functions - the only difference being that the access levels are not explicitly specified.&lt;br /&gt;
&lt;br /&gt;
Access control has evolved over time to become highly complicated &amp;lt;ref&amp;gt;http://www.exforsys.com/tutorials/oops/the-use-of-access-specifiers-in-object-oriented-programming.html&amp;lt;/ref&amp;gt; because, the requirements of computing and software development have ballooned considerably in last decade and a half. Complex software requirements mandates complexity in languages, and this has indeed affected the access control policies, which have evolved into something comprehensive, yet - at times - convoluted. Unfortunately today's developers often lose sight of the fundamental motivation behind the existence of extensive access control and lose out on much of the benefits of having a highly cohesive, low coupled system&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Coupling_(computer_programming)&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To elaborate, it has become somewhat of an involuntary practice for present programmers to make ''all'' data members of a class private and provide accessor methods (getters/setters) to all of them (data members). Developers have come to accept this to be a contemporary programmatic-paradigm and by supplying setters and getters for ''everything'', defeat the very purpose of making class-fields private. A long time ago programmers discovered that reducing the scope (visibility) of data as much as possible leads to more reliable and maintainable code&amp;lt;ref&amp;gt;http://typicalprogrammer.com/?p=23&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Every getter and setter in one’s code represents a failure to encapsulate and creates unnecessary coupling. A profusion of getters and setters is a sign of a poorly-designed set of classes. Java&amp;lt;ref&amp;gt;http://www.eclipse-blog.org/eclipse-ide/auto-generating-getters-and-setters.html&amp;lt;/ref&amp;gt; and C#&amp;lt;ref&amp;gt;http://forums.asp.net/t/941864.aspx/1&amp;lt;/ref&amp;gt; developers have IDEs that generate getters and setters automatically, implying that accessors are a good idea and thus contribute to that popular belief. That is not to say that one must completely abstain from accessors, but rather re-analyze and - if required - redesign the class structure to involve as little exposure of its data members as can be achieved.&lt;br /&gt;
&lt;br /&gt;
==Access Control: A typical example==&lt;br /&gt;
Empirically, the data members of a class are held private, and the member functions (public) act as the end points of this class. Via these public methods, one can access the ‘state’ of an object. The following example illustrates this point and shows how access to the data members of a class are specified and controlled by the access modifiers.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/UML_Diagram#Diagrams_overview UML Diagram] of a BankAccount Class.&lt;br /&gt;
&lt;br /&gt;
[[Image:BankAccount_UML.png|650px|thumb|center|Typical UML diagram of BankAccount.]]&lt;br /&gt;
&lt;br /&gt;
The “(-)/(+)” preceding the members of this class represent the associated access levels. (-) indicates private and (+) indicates public.&lt;br /&gt;
The data members ‘accountNumber’, ‘routingNumber’ and ‘balance’ are maintained as private. Thus, an instance of a ‘Customer’ Object (say) cannot directly access or modify this data. Customer has to interact with the public behavior bundled in this module which allows him to view his account information and/or withdraw/deposit money i.e. his visibility of the class is only restricted to that which is made public by the Class. &lt;br /&gt;
&lt;br /&gt;
Using such access specifiers, we can control the level of security and visibility associated with the members of a class. Thus, an Access Specifier can be roughly defined as ''‘A keyword applied to a variable, method, etc. that indicates which other parts of the code are permitted to access it’'' &amp;lt;ref&amp;gt;http://en.wiktionary.org/wiki/access_specifier&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Common Access Specifiers in O-O languages==&lt;br /&gt;
&lt;br /&gt;
Although all access specifiers essentially provide the same functionality, their semantics  differ among different O-O languages. Here is how access control is implemented in some of the more common O-O languages.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
*'''private''':All members of the Class that are declared as private can only be accessed by the class's member functions and [http://en.wikipedia.org/wiki/Friend_class friends] of the class.&lt;br /&gt;
*'''protected''':All members of the Class that are declared as protected can be accessed by that class's member functions and friends (classes or functions) of the class. Moreover, classes derived from the class also have access to these elements.&lt;br /&gt;
*'''public''':Any class or method can have access to these type of elements.&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Java offers four access specifiers, listed below in order of decreasing accessibility: &lt;br /&gt;
*'''public''': All public classes, methods, and fields are the least secure. The Public access specifier must be used if you explicitly want to offer access to these entities and if this access cannot do any conceivable harm.&lt;br /&gt;
*'''protected''': Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes present in the same package. Use of protected access specifier thus only allows its fields to be accessed by those classes which have some semblance of correlation to it - be it in the form of subclasses (correlation through inheritance) or classes within the same package (logical and/or functional correlation i.e. classes in the same package have ''something'' logical/functional common to them).&lt;br /&gt;
*'''default '''(no specifier): Default access specifier is Java’s fail-safe way to handle security. Via the default access specifier, a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. Default access is thus suited when the source code is compartmentalized and organized into packages.&lt;br /&gt;
*'''private''': Private access specifier is a way to secure vital members of a class.  Private methods and fields can only be accessed within the same class to which the methods and fields belong. Private methods and fields are not visible within subclasses and are not inherited by subclasses.&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Ruby gives you three levels of protection, listed below in order of decreasing accessibility:&lt;br /&gt;
*'''public''':Public methods can be called by everyone - no access control is enforced. A class's instance methods are public by default. &lt;br /&gt;
*'''protected''':Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.&lt;br /&gt;
*'''private''':Private methods cannot be called with an explicit receiver - the receiver is always self. Thus private methods can be called only in the context of the current object and cannot be invoked by another object's private methods. &lt;br /&gt;
&lt;br /&gt;
Access control in Ruby is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method&amp;lt;ref&amp;gt;http://rubylearning.com/satishtalim/ruby_access_control.html&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
The following access modifiers specify the accessibility levels in C#&lt;br /&gt;
*'''public''':Any public method or member can be accessed by any other code in the same  or different package.&lt;br /&gt;
*'''private''':The method or member can only be accessed from within the same class.&lt;br /&gt;
*'''protected''':The method or member can be accessed only from within the same Class or from within any of the subclasses.&lt;br /&gt;
*'''internal''':The type or member can be accessed by any code in the same assembly, but not from another assembly. An assembly in C# is the rough equivalent of a package in java.&lt;br /&gt;
*'''protected internal''':The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Summary of Access Specfiers in C++, Java, Ruby and C#:===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Access Level&lt;br /&gt;
! C++&lt;br /&gt;
! Java&lt;br /&gt;
! Ruby&lt;br /&gt;
! C#&lt;br /&gt;
|-&lt;br /&gt;
! Public&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
| Anything can access&lt;br /&gt;
|-&lt;br /&gt;
! Protected&lt;br /&gt;
|  Class, Subclasses and friends&lt;br /&gt;
|  Class, Subclasses and classes of same package&lt;br /&gt;
|  Class and Subclasses&lt;br /&gt;
| Class and Subclasses&lt;br /&gt;
|-&lt;br /&gt;
! Private&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
| Within Class&lt;br /&gt;
|-&lt;br /&gt;
! Internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly&lt;br /&gt;
|-&lt;br /&gt;
! Protected internal&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| --&lt;br /&gt;
| Any Class from same Assembly and Subclass from any Assembly.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Examples of Access Specifiers==&lt;br /&gt;
The following examples exhibit the use of the access specifiers and getter methofs for the aforementioned Bank Account example.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount {&lt;br /&gt;
	private:&lt;br /&gt;
	char routingNumber;&lt;br /&gt;
	char accountNumber;&lt;br /&gt;
	int  balance;&lt;br /&gt;
	&lt;br /&gt;
	public:&lt;br /&gt;
	BankAccount(char *, char *);&lt;br /&gt;
	void changeAccountNumber(char *);&lt;br /&gt;
	void changeRoutingtNumber(char *);&lt;br /&gt;
	void depositMoney(int);&lt;br /&gt;
	void withdrawMoney(int);&lt;br /&gt;
	void displayAccountInfo();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::BankAccount(char *rNo, char *accNo) {&lt;br /&gt;
	Strcpy(routingNumber, rNo);&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
	balance = 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeAccountNumber(char *acNo){&lt;br /&gt;
	Strcpy(accountNumber, accNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void changeRoutingtNumber(char *rNo){&lt;br /&gt;
	Strcpy(routingNumber, arNo);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void depositMoney(int amount) {&lt;br /&gt;
	balance += amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::void withdrawMoney(int amount) {&lt;br /&gt;
	balance -= amount;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
BankAccount::displayAccountInfo() {&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Number:” &amp;lt;&amp;lt; accountNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Routing Number:” &amp;lt;&amp;lt; routingNumber &amp;lt;&amp;lt; endl;&lt;br /&gt;
	cout &amp;lt;&amp;lt; “Account Balance:” &amp;lt;&amp;lt; balance &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the datamambers routingNumber, accountNumber and balance are made private. This makes it impossible to directly access  or change these data members from outside code. Access to these attributes are strictly through the public methods changeRoutingNumber() and changeAccountNumber(). Thus, it purely at the discretion of the programmer to allow access to these data members through such accessor functions. In the absence of such accessors, there would be no way for any code outside the class to view/modify the private attributes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BankAccount {&lt;br /&gt;
	String accountNumber;&lt;br /&gt;
	private String routingNumber;&lt;br /&gt;
	private Integer balance;&lt;br /&gt;
	&lt;br /&gt;
	public void setAccountNumber(String acNo){&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void setRoutingNumber(String rtNo){&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected void setBalance(Integer bal){&lt;br /&gt;
		this.balance=bal;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getAccountNumber(){&lt;br /&gt;
		return this.accountNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public String getRoutingNumber(){&lt;br /&gt;
		return this.routingNumber;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected Integer getBalance(){&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showAccountNumber(){&lt;br /&gt;
		System.out.println(this.accountNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showRoutingNumber(){&lt;br /&gt;
		System.out.println(this.routingNumber);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void showBalance(){&lt;br /&gt;
		System.out.println(this.balance);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void withdraw(Integer amount){&lt;br /&gt;
		this.balance-=amount;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public void deposit(Integer amount){&lt;br /&gt;
		this.balance+=amount;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]){&lt;br /&gt;
		BankAccount Steve = new BankAccount();&lt;br /&gt;
		&lt;br /&gt;
		//public access to setRoutingNumber/getRoutingNumber&lt;br /&gt;
		Steve.setRoutingNumber(&amp;quot;1234&amp;quot;);&lt;br /&gt;
		String steveRtNo=Steve.getRoutingNumber();&lt;br /&gt;
		System.out.println(steveRtNo);&lt;br /&gt;
		&lt;br /&gt;
		//protected access to the method setBalance/getBalance&lt;br /&gt;
		Steve.setBalance(5000);&lt;br /&gt;
		System.out.println(Steve.getBalance());		&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Class BankAccount in the above Java code has public getter/setter methods to access the private data member ‘routingNumber’. Thus, we can invoke these methods using an instance of the BankAccount object: Steve.&lt;br /&gt;
The methods setBalance()/ getBalance() are protected. As can be seen, they behave like public methods if invoked in the same package. Protected methods act like private methods if invoked by any instance of a class outside the package.&lt;br /&gt;
However, if there is a class ‘InternationalBankAccount’ in a different package such that:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class InternationalBankAccount extends BankAccount {&lt;br /&gt;
	public String internationalBankAccNo;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
InternationalBankAccount Anna = new InternationalBankAccount();&lt;br /&gt;
Anna.setBalance(10000);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above is a valid syntax and Anna can access protected member functions of the parent class.&lt;br /&gt;
Data member ‘accountNumber’ has default access specifier. Default access specifier is used in Java to support package driven development. Thus ‘accountNumber’ can be accessed from anywhere within the package. However no classes from other packages can access ‘accountNumber’.&lt;br /&gt;
Data member balance is a private data member and if it is invoked explicitly as follows,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve.balance=1000;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
then the output would be an unresolved compilation error of the form:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Unresolved compilation problem: The field bankAccount.balance is not visible&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
The following code illustrates the use of access specifiers in Ruby. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount  &lt;br /&gt;
&lt;br /&gt;
    def initialize(acNo, rtNo, bal)  &lt;br /&gt;
      @accountNumber = acNo&lt;br /&gt;
      @routingNumber = rtNo&lt;br /&gt;
     @balance = bal&lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    def balance&lt;br /&gt;
      @balance&lt;br /&gt;
    end &lt;br /&gt;
&lt;br /&gt;
    def routingNumber&lt;br /&gt;
      @routingNumber&lt;br /&gt;
     end &lt;br /&gt;
&lt;br /&gt;
    def accountNumber&lt;br /&gt;
      @accountNumber&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
    def compare_balance(other)  &lt;br /&gt;
      if other.balance &amp;gt; balance   &lt;br /&gt;
        &amp;quot;The other object has more bank balance.&amp;quot;  &lt;br /&gt;
      else  &lt;br /&gt;
        &amp;quot;This object has more bank balance.&amp;quot;  &lt;br /&gt;
      end  &lt;br /&gt;
    end  &lt;br /&gt;
&lt;br /&gt;
    protected:balance&lt;br /&gt;
    public:routingNumber&lt;br /&gt;
    private:accountNumber  &lt;br /&gt;
&lt;br /&gt;
end  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Steve = BankAccount.new(713, 123, 1000)  &lt;br /&gt;
Anna  = BankAccount.new(923, 125, 2000)  &lt;br /&gt;
puts Steve.routingNumber()  &lt;br /&gt;
puts Steve.compare_balance (Anna) &lt;br /&gt;
puts Steve.accountNumber()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
123&lt;br /&gt;
The other object has more bank balance.&lt;br /&gt;
NoMethodError: private method `accountNumber' called for #&amp;lt;BankAccount:0x2b75320&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
‘routingNumber’ is a public function. Thus, when we access object Steve’s public method ‘routing number’, we successfully get ‘123’ as the output.&lt;br /&gt;
Furthermore, this snippet provides a mechanism for the comparison of one account holder’s balance with another. This comparison involves a call to the method ‘balance’. The object performing the comparison has to ask the other object to execute its balance method. So, balance cannot be private. With ‘balance’ changed to protected instead of private, Steve can ask Anna to execute ‘balance’, because both are instances of the same class. But if we try to call the ‘balance’ method of a bankAccount object when ‘self’ is anything other than a bankAccount object, the method will fail. A protected method is thus just like a private method, but with the exemption for those cases where the class of ‘self’ and the class of the respective object (having the method called on it) are the same.&lt;br /&gt;
‘accountNumber’ method is private. Thus user gets a ‘NoMethodError’ on invoking object Steve’s ‘accountNumber’ method. Initialize method in a class is private by default. Whenever the new method is invoked, initialize method is called and instances are created. If we try to invoke the ‘initialize’ method explicitly, we get the same error as in the case when we tried to invoke object Steve’s private ‘accountNumber’ method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===C#===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class BankAccount &lt;br /&gt;
{&lt;br /&gt;
	public BankAccount(string accountNumber, string routingNumber, int balance)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		this.balance = balance;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected string accountNumber;&lt;br /&gt;
	protected string routingNumber;&lt;br /&gt;
	private int balance;&lt;br /&gt;
	&lt;br /&gt;
	private void setAccountNumber(string acNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber=acNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private void setRoutingNumber(string rtNo)&lt;br /&gt;
	{&lt;br /&gt;
		this.routingNumber=rtNo;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	protected initBalance()&lt;br /&gt;
	{&lt;br /&gt;
		this.balance = 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void getBalance(int balance)&lt;br /&gt;
	{&lt;br /&gt;
		return this.balance;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Customer : BankAccount&lt;br /&gt;
{&lt;br /&gt;
	Customer(string accountNumber, string routingNumber)&lt;br /&gt;
	{&lt;br /&gt;
		this.accountNumber = accountNumber;&lt;br /&gt;
		this.routingNumber = routingNumber;&lt;br /&gt;
		initBalance();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Startup&lt;br /&gt;
{&lt;br /&gt;
	public static void main()&lt;br /&gt;
	{	&lt;br /&gt;
		Customer cust = new Customer(“999”, “888”, 100);&lt;br /&gt;
		System.console.WriteLine(cust.getBalance);	&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Customer class inherits from the BankAccount class. All protected data members and methods of BankAccount class are accessible directly by an object of the Customer class. The private fields such as accountNumber and routingNumber are not directly accessible. Thus it is possible to call only the initBalance and getBalance methods from Customer. The initBalance method cannot be called by anyone outside the BankAccount class.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Access control is an integral component of object oriented languages and it underlines and emphasizes some the pricipal ideals central to the object-oriented approach. It is possible to maximize the benefits of any O-O language by adhering to, and fully exploiting the capabilities of its access specifiers. Access control is in no way a peripheral add-on or redundant feature of O-O languages, and programmers would only stand to gain by fully appreciating, respecting and understanding their presence.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sshyamr</name></author>
	</entry>
</feed>