<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-interfaces" data-graded="True">
<h2 class="hd hd-2 unit-title">Reading 11 Objectives</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_404696e3c261">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-block-type="video" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_404696e3c261" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_404696e3c261"
class="video closed"
data-metadata='{"completionEnabled": false, "start": 0.0, "saveStateEnabled": false, "lmsRootURL": "https://openlearninglibrary.mit.edu", "showCaptions": "true", "captionDataDir": null, "poster": "https://mitx-edx-video-meta-storage.s3.amazonaws.com/media/video-images/708d9cc058e2468c830cdc5b373f981e.png?Signature=0GzWBO2%2FOVS%2Fzq8%2BaT9bjUVIs6k%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBkaCXVzLWVhc3QtMSJHMEUCIDYJr1IINE4yTR9iPEwG2y725FxPSyJWkNdmTtFqlQmUAiEA%2Bp2QM8A3RY0G7acre%2FKqkRgF6%2BV0oIxUO3uGrW886YwqxAUI8v%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARADGgw4NTkyMDU2Mzc0MTEiDDagke3km0hmDa5ZhyqYBftqa%2F7B2EY2kt59YkE6P4Nich9mAuaxqEIE0KSnI9KUvWhmSuD5abolsXiI4tiZWvVhLClGsEtWpe%2Be%2BJFMwFzwYJLnnkF7EULNdQTqSxtfcLVSl%2B69VRZXqJaDPcUrgF4wWeMPKNNzYij2x6lVIFNsl2a4d1YKKFKVSXmNhFMqM%2Btj31kdGyU8FHYzpfOiS20U%2Bf6YGZU81c2g9%2FvjxiwBuKpEPb58bhbZOZmkPbJArmuOAtAsjOg0Yqu%2Bk9hmHZdfOx94B2Qn5PHIqgPPdVBEUfF%2BVy7eqV4NzphcddZ157j%2FfQx948%2BCrqIDRZdkvI8H%2FzJj%2FNZj8r3NHN1H%2FJtRlb2edWm6tpgkQNMZaID1QTZZQPiARor8y%2BlW%2BPCbl2AcuE%2B8SBA8Gl%2FPZkxRNT86yy5JPBgRpdHUG8hLNmD0s1lCUex7arUR%2BSbklUk0TH%2BNI4%2BZBTnB3slf6saTHcwmnEvcSOhzavdH0Yw3R0y4%2B6XOEOK9p4239vPVA6bxiSQD6FYOfPT9W8V7o1A66SUXPWqVqrAm06UtC%2FncnrR6hAd1FR4zsh4luqogzLApdMq4Hgvw8as22NbOYpkE7nXHz7OX0T8TwVYtSZZDtVGpiVQeEhViIA35Z1%2F06HBxNxRl%2FzhjVpfSb2Tm2%2B4EoajjVboxR%2BCSjKpa7XaX6pFyQuvb2Tob4nYZDSiI4JT96s8iBNJFdO%2FaSY0n0kIJdkYK1xzRu7jRosOkKkPyZoMyCkqi%2FP%2F2MCadw%2FO7PY3HpwN3PzlxDpX1OjapY8IJ8YFhurxeURK6%2FpG5il2ZOjWhR4qA368DcUE9smfCqHZfdWvCqmemKdkzfqzvCpMRZgj4i6hk1HRdP%2FbSrjb8GtpGB8w931Qa480wg8WUtQY6sQH%2FWTcvrVAJNdwOn6GrmiKVTrC4NtXmQjnsE3B8wlpYPlqx8leM7PpWKSykNgIljGkEUZiE9uZ9jAUIrGHcpZGUoa9DYysov6bGRRmWMjNZ5yZby4X4XaivItRQkUbhrlKa%2BE6d2nVzWcmPMDvqj9%2BZ5yr4mQ3m96AfUnt27oj3X2MoQSlWrPzPWc63fFg1swzqgrvPxuV88F8GsDvQe8oqQ57sIVZF1IYNawKB%2B0BWM6I%3D&Expires=1722099516&AWSAccessKeyId=ASIA4QDFFSUR4AISCAFW", "streams": "1.00:9cwa3USuVXQ", "ytApiUrl": "https://www.youtube.com/iframe_api", "transcriptLanguages": {"en": "English"}, "duration": 20.35, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V005900_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V005900/MIT600512016-V005900.m3u8"], "recordedYoutubeIsAvailable": true, "prioritizeHls": false, "autoplay": false, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_404696e3c261/handler/transcript/available_translations", "autoAdvance": false, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_404696e3c261/handler/transcript/translation/__lang__", "generalSpeed": 1.0, "savedVideoPosition": 0.0, "completionPercentage": 0.95, "speed": null, "autohideHtml5": false, "end": 0.0, "ytMetadataEndpoint": "", "transcriptLanguage": "en", "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_404696e3c261/handler/publish_completion", "ytTestTimeout": 1500, "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_404696e3c261/handler/xmodule_handler/save_user_state"}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_404696e3c261"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_404696e3c261">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_404696e3c261">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_404696e3c261/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_404696e3c261/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_99be9b7ae8ee">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_99be9b7ae8ee" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h4>Software in 6.005</h4>
<table class="table table-striped no-markdown">
<tbody>
<tr>
<th width="33%">Safe from bugs</th>
<th>Easy to understand</th>
<th>Ready for change</th>
</tr>
<tr>
<td>
Correct today and correct in the unknown future
</td>
<td>
Communicating clearly with future programmers, including future you
</td>
<td>
Designed to accommodate change without rewriting
</td>
</tr>
</tbody>
</table>
<h4>Objectives for Today</h4>
<p>The topic of today's reading is interfaces: separating the interface of an abstract data type from its implementation, and using Java <code>interface</code> types to enforce that separation.</p>
<p>After today's reading, you should be able to define ADTs with interfaces, and write classes that implement interfaces.</p>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Interfaces" data-graded="True">
<h2 class="hd hd-2 unit-title">Interfaces</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_e4d2a968baeb">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-block-type="video" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_e4d2a968baeb" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_e4d2a968baeb"
class="video closed"
data-metadata='{"completionEnabled": false, "start": 0.0, "saveStateEnabled": false, "lmsRootURL": "https://openlearninglibrary.mit.edu", "showCaptions": "true", "captionDataDir": null, "poster": null, "streams": "1.00:xe1c3-_vorA", "ytApiUrl": "https://www.youtube.com/iframe_api", "transcriptLanguages": {"en": "English"}, "duration": 113.38, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V006500_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V006500/MIT600512016-V006500.m3u8"], "recordedYoutubeIsAvailable": true, "prioritizeHls": false, "autoplay": false, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_e4d2a968baeb/handler/transcript/available_translations", "autoAdvance": false, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_e4d2a968baeb/handler/transcript/translation/__lang__", "generalSpeed": 1.0, "savedVideoPosition": 0.0, "completionPercentage": 0.95, "speed": null, "autohideHtml5": false, "end": 0.0, "ytMetadataEndpoint": "", "transcriptLanguage": "en", "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_e4d2a968baeb/handler/publish_completion", "ytTestTimeout": 1500, "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_e4d2a968baeb/handler/xmodule_handler/save_user_state"}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_e4d2a968baeb"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_e4d2a968baeb">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_e4d2a968baeb">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_e4d2a968baeb/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_e4d2a968baeb/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_8ff68423c1e5">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_8ff68423c1e5" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="interfaces">Interfaces</h2>
<div data-outline="interfaces">
<p>Java's <code>interface</code> is a useful language mechanism for expressing an abstract data type. An interface in Java is a list of method signatures, but no method bodies. A class <em>implements</em> an interface if it declares the interface in its <code>implements</code> clause, and provides method bodies for all of the interface's methods. So one way to define an abstract data type in Java is as an interface, with its implementation as a class implementing that interface.</p>
<p>One advantage of this approach is that the interface specifies the contract for the client and nothing more. The interface is all a client programmer needs to read to understand the ADT. The client can't create inadvertent dependencies on the ADT's rep, because instance variables can't be put in an interface at all. The implementation is kept well and truly separated, in a different class altogether.</p>
<p>Another advantage is that multiple different representations of the abstract data type can co-exist in the same program, as different classes implementing the interface. When an abstract data type is represented just as a single class, without an interface, it's harder to have multiple representations. In the <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-designing-an-abstract-type#example_different_representations_for_strings"><code>MyString</code> example from <em>Abstract Data Types</em></a>, <code>MyString</code> was a single class. We explored two different representations for <code>MyString</code>, but we couldn't have both representations for the ADT in the same program.</p>
<p>Java's static type checking allows the compiler to catch many mistakes in implementing an ADT's contract. For instance, it is a compile-time error to omit one of the required methods, or to give a method the wrong return type. Unfortunately, the compiler doesn't check for us that the code adheres to the specs of those methods that are written in documentation comments.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_af410255be21" data-graded="True">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-interfaces-35">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-interfaces-35" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-interfaces-35" class="problems-wrapper" role="group"
aria-labelledby="11-interfaces-35-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-interfaces-35" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-interfaces-35/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-interfaces-35-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-interfaces-35-problem-progress" tabindex="-1">
interfaces
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-interfaces-35-problem-progress"></div>
<div class="problem">
<div>
<p>Consider this Java interface and Java class, which are intended to implement an immutable set data type:</p>
<pre class="code">
/** Represents an immutable set of elements of type E. */
interface Set&lt;E&gt; {
/** make an empty set */
/*A*/ public Set();
/** @return true if this set contains e as a member */
public boolean contains(E e);
/** @return a set which is the union of this and that */
/*B*/ public ArraySet&lt;E&gt; union(Set&lt;E&gt; that);
}
/** Implementation of Set&lt;E&gt;. */
class ArraySet&lt;E&gt; implements Set&lt;E&gt; {
/** make an empty set */
public ArraySet() { ... }
/** @return a set which is the union of this and that */
public ArraySet&lt;E&gt; union(Set&lt;E&gt; that) { ... }
/** add e to this set */
public void add(E e) { ... }
}
</pre>
<p>Which of the following statements are true about Set&lt;E&gt; and ArraySet&lt;E&gt;? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-interfaces-35_2_1">
<fieldset aria-describedby="status_11-interfaces-35_2_1">
<div class="field">
<input type="checkbox" name="input_11-interfaces-35_2_1[]" id="input_11-interfaces-35_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="11-interfaces-35_2_1-choice_0-label" for="input_11-interfaces-35_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-interfaces-35_2_1"> The line labeled /*A*/ is a problem because Java interfaces can't have constructors.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-interfaces-35_2_1[]" id="input_11-interfaces-35_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="11-interfaces-35_2_1-choice_1-label" for="input_11-interfaces-35_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-interfaces-35_2_1"> The line labeled /*B*/ is a problem because Set mentions ArraySet, but ArraySet also mentions Set, which is circular.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-interfaces-35_2_1[]" id="input_11-interfaces-35_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="11-interfaces-35_2_1-choice_2-label" for="input_11-interfaces-35_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-interfaces-35_2_1"> The line labeled /*B*/ is a problem because it isn't representation-independent.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-interfaces-35_2_1[]" id="input_11-interfaces-35_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="11-interfaces-35_2_1-choice_3-label" for="input_11-interfaces-35_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_11-interfaces-35_2_1"> ArraySet doesn't correctly implement Set because it's missing the contains() method.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-interfaces-35_2_1[]" id="input_11-interfaces-35_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="11-interfaces-35_2_1-choice_4-label" for="input_11-interfaces-35_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_11-interfaces-35_2_1"> ArraySet doesn't correctly implement Set because it includes a method that Set doesn't have.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-interfaces-35_2_1[]" id="input_11-interfaces-35_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="11-interfaces-35_2_1-choice_5-label" for="input_11-interfaces-35_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_11-interfaces-35_2_1"> ArraySet doesn't correctly implement Set because ArraySet is mutable while Set is immutable.
</label>
</div>
<span id="answer_11-interfaces-35_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-interfaces-35_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_11-interfaces-35_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="interfaces" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-interfaces-35" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-interfaces-35">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-interfaces-35-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-interfaces-35-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-interfaces-35-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Subtypes" data-graded="True">
<h2 class="hd hd-2 unit-title">Subtypes</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6560003a16d1">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-block-type="video" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6560003a16d1" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_6560003a16d1"
class="video closed"
data-metadata='{"completionEnabled": false, "start": 0.0, "saveStateEnabled": false, "lmsRootURL": "https://openlearninglibrary.mit.edu", "showCaptions": "true", "captionDataDir": null, "poster": null, "streams": "1.00:BgUx7Op8yEE", "ytApiUrl": "https://www.youtube.com/iframe_api", "transcriptLanguages": {"en": "English"}, "duration": 146.8, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V006900_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V006900/MIT600512016-V006900.m3u8"], "recordedYoutubeIsAvailable": true, "prioritizeHls": false, "autoplay": false, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6560003a16d1/handler/transcript/available_translations", "autoAdvance": false, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6560003a16d1/handler/transcript/translation/__lang__", "generalSpeed": 1.0, "savedVideoPosition": 0.0, "completionPercentage": 0.95, "speed": null, "autohideHtml5": false, "end": 0.0, "ytMetadataEndpoint": "", "transcriptLanguage": "en", "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6560003a16d1/handler/publish_completion", "ytTestTimeout": 1500, "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6560003a16d1/handler/xmodule_handler/save_user_state"}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_6560003a16d1"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_6560003a16d1">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_6560003a16d1">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6560003a16d1/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6560003a16d1/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_581d4b1819c8">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_581d4b1819c8" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="subtypes">Subtypes</h2>
<div data-outline="subtypes"><p>Recall that a <em>type</em> is a set of values.
The Java <a href="http://docs.oracle.com/javase/8/docs/api/?java/util/List.html"><code>List</code></a> type is defined by an interface.
If we think about all possible <code>List</code> values, none of them are <code>List</code> objects: we cannot create instances of an interface.
Instead, those values are all <code>ArrayList</code> objects, or <code>LinkedList</code> objects, or objects of another class that implements <code>List</code>.
A <em>subtype</em> is simply a subset of the <em>supertype</em>: <code>ArrayList</code> and <code>LinkedList</code> are subtypes of <code>List</code>.</p><p>“B is a subtype of A” means “every B is an A.”
In terms of specifications: “every B satisfies the specification for A.”</p><p>That means B is only a subtype of A if B’s specification is at least as strong as A’s specification.
When we declare a class that implements an interface, the Java compiler enforces part of this requirement automatically: for example, it ensures that every method in A appears in B, with a compatible type signature.
Class B cannot implement interface A without implementing all of the methods declared in A.</p><p>But the compiler cannot check that we haven’t weakened the specification in other ways: strengthening the precondition on some inputs to a method, weakening a postcondition, weakening a guarantee that the interface abstract type advertises to clients.
If you declare a subtype in Java — implementing an interface is our current focus — then you must ensure that the subtype’s spec is at least as strong as the supertype’s.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_2f534aca9959" data-graded="True">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-immutable-shapes">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-immutable-shapes" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-immutable-shapes" class="problems-wrapper" role="group"
aria-labelledby="11-immutable-shapes-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-immutable-shapes" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-immutable-shapes/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="3"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-immutable-shapes-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-immutable-shapes-problem-progress" tabindex="-1">
immutable shapes
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-immutable-shapes-problem-progress"></div>
<div class="problem">
<div>
<p>Let's define an interface for rectangles:</p>
<pre class="code">
/** An immutable rectangle. */
public interface ImmutableRectangle {
/** @return the width of this rectangle */
public int getWidth();
/** @return the height of this rectangle */
public int getHeight();
}
</pre>
<p>It follows that every square is a rectangle:</p>
<pre class="code">
/** An immutable square. */
public class ImmutableSquare {
private final int side;
/** Make a new side x side square. */
public ImmutableSquare(int side) { this.side = side; }
/** @return the width of this square */
public int getWidth() { return side; }
/** @return the height of this square */
public int getHeight() { return side; }
}
</pre>
<p>Does <code>ImmutableSquare.getWidth()</code> satisfy the spec of <code>ImmutableRectangle.getWidth()</code>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<select name="input_11-immutable-shapes_2_1" id="input_11-immutable-shapes_2_1" aria-describedby="status_11-immutable-shapes_2_1">
<option value="option_11-immutable-shapes_2_1_dummy_default">Select an option</option>
<option value="Yes"> Yes</option>
<option value="No"> No</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-immutable-shapes_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-immutable-shapes_2_1"/>
</div></div>
<p>Does <code>ImmutableSquare.getHeight()</code> satisfy the spec of <code>ImmutableRectangle.getHeight()</code>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<select name="input_11-immutable-shapes_3_1" id="input_11-immutable-shapes_3_1" aria-describedby="status_11-immutable-shapes_3_1">
<option value="option_11-immutable-shapes_3_1_dummy_default">Select an option</option>
<option value="Yes"> Yes</option>
<option value="No"> No</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-immutable-shapes_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-immutable-shapes_3_1"/>
</div></div>
<p>Does the whole <code>ImmutableSquare</code> spec satisfy the <code>ImmutableRectangle</code> spec?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<select name="input_11-immutable-shapes_4_1" id="input_11-immutable-shapes_4_1" aria-describedby="status_11-immutable-shapes_4_1">
<option value="option_11-immutable-shapes_4_1_dummy_default">Select an option</option>
<option value="Yes"> Yes</option>
<option value="No"> No</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-immutable-shapes_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-immutable-shapes_4_1"/>
</div></div>
<div class="solution-span">
<span id="solution_11-immutable-shapes_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="immutable shapes" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-immutable-shapes" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-immutable-shapes">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-immutable-shapes-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-immutable-shapes-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-immutable-shapes-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-mutable-shapes">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-mutable-shapes" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-mutable-shapes" class="problems-wrapper" role="group"
aria-labelledby="11-mutable-shapes-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-mutable-shapes" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-mutable-shapes/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="4"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-mutable-shapes-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-mutable-shapes-problem-progress" tabindex="-1">
mutable shapes
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-mutable-shapes-problem-progress"></div>
<div class="problem">
<div>
<p>Now let's consider a mutable rectangle:</p>
<pre class="code">
/** An mutable rectangle. */
public interface MutableRectangle {
// ... same methods as above ...
/** Set this rectangle's dimensions to width x height. */
public void setSize(int width, int height);
</pre>
<p>Surely every square is still a rectangle?</p>
<pre class="code">
/** A mutable square. */
public class MutableSquare {
private final int side;
// ... same constructor and methods as above ...
// TODO implement setSize(..)
</pre>
<p>For each possible <code>MutableSquare.setSize(..)</code> implementation below, is it a valid implementation?</p>
<p>1.</p>
<pre class="code">
/** Set this square's dimensions to width x height.
* Requires width = height. */
public void setSize(int width, int height) { ... }
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<select name="input_11-mutable-shapes_2_1" id="input_11-mutable-shapes_2_1" aria-describedby="status_11-mutable-shapes_2_1">
<option value="option_11-mutable-shapes_2_1_dummy_default">Select an option</option>
<option value="No -- stronger precondition"> No -- stronger precondition</option>
<option value="Yes -- weaker precondition"> Yes -- weaker precondition</option>
<option value="Yes -- stronger postcondition"> Yes -- stronger postcondition</option>
<option value="No -- weaker postcondition"> No -- weaker postcondition</option>
<option value="Specifications are incomparable"> Specifications are incomparable</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-mutable-shapes_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-mutable-shapes_2_1"/>
</div></div>
<p>2.</p>
<pre class="code">
/** Set this square's dimensions to width x height.
* @throw BadSizeException if width != height */
public void setSize(int width, int height) throws BadSizeException { ... }
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<select name="input_11-mutable-shapes_3_1" id="input_11-mutable-shapes_3_1" aria-describedby="status_11-mutable-shapes_3_1">
<option value="option_11-mutable-shapes_3_1_dummy_default">Select an option</option>
<option value="No -- stronger precondition"> No -- stronger precondition</option>
<option value="Yes -- weaker precondition"> Yes -- weaker precondition</option>
<option value="Yes -- stronger postcondition"> Yes -- stronger postcondition</option>
<option value="No -- weaker postcondition"> No -- weaker postcondition</option>
<option value="Specifications are incomparable"> Specifications are incomparable</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-mutable-shapes_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-mutable-shapes_3_1"/>
</div></div>
<p>3.</p>
<pre class="code">
/** If width = height, set this square's dimensions to width x height.
* Otherwise, new dimensions are unspecified. */
public void setSize(int width, int height) { ... }
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<select name="input_11-mutable-shapes_4_1" id="input_11-mutable-shapes_4_1" aria-describedby="status_11-mutable-shapes_4_1">
<option value="option_11-mutable-shapes_4_1_dummy_default">Select an option</option>
<option value="No -- stronger precondition"> No -- stronger precondition</option>
<option value="Yes -- weaker precondition"> Yes -- weaker precondition</option>
<option value="Yes -- stronger postcondition"> Yes -- stronger postcondition</option>
<option value="No -- weaker postcondition"> No -- weaker postcondition</option>
<option value="Specifications are incomparable"> Specifications are incomparable</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-mutable-shapes_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-mutable-shapes_4_1"/>
</div></div>
<p>4.</p>
<pre class="code">
/** Set this square's dimensions to side x side. */
public void setSize(int side) { ... }
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="inputtype option-input ">
<select name="input_11-mutable-shapes_5_1" id="input_11-mutable-shapes_5_1" aria-describedby="status_11-mutable-shapes_5_1">
<option value="option_11-mutable-shapes_5_1_dummy_default">Select an option</option>
<option value="No -- stronger precondition"> No -- stronger precondition</option>
<option value="Yes -- weaker precondition"> Yes -- weaker precondition</option>
<option value="Yes -- stronger postcondition"> Yes -- stronger postcondition</option>
<option value="No -- weaker postcondition"> No -- weaker postcondition</option>
<option value="Specifications are incomparable"> Specifications are incomparable</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-mutable-shapes_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-mutable-shapes_5_1"/>
</div></div>
<div class="solution-span">
<span id="solution_11-mutable-shapes_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="mutable shapes" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-mutable-shapes" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-mutable-shapes">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-mutable-shapes-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-mutable-shapes-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-mutable-shapes-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Examples" data-graded="True">
<h2 class="hd hd-2 unit-title">Examples</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_053b7a396de5">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-block-type="video" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_053b7a396de5" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_053b7a396de5"
class="video closed"
data-metadata='{"completionEnabled": false, "start": 0.0, "saveStateEnabled": false, "lmsRootURL": "https://openlearninglibrary.mit.edu", "showCaptions": "true", "captionDataDir": null, "poster": null, "streams": "1.00:FA55le1x8KE", "ytApiUrl": "https://www.youtube.com/iframe_api", "transcriptLanguages": {"en": "English"}, "duration": 404.14, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V006600_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V006600/MIT600512016-V006600.m3u8"], "recordedYoutubeIsAvailable": true, "prioritizeHls": false, "autoplay": false, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_053b7a396de5/handler/transcript/available_translations", "autoAdvance": false, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_053b7a396de5/handler/transcript/translation/__lang__", "generalSpeed": 1.0, "savedVideoPosition": 0.0, "completionPercentage": 0.95, "speed": null, "autohideHtml5": false, "end": 0.0, "ytMetadataEndpoint": "", "transcriptLanguage": "en", "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_053b7a396de5/handler/publish_completion", "ytTestTimeout": 1500, "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_053b7a396de5/handler/xmodule_handler/save_user_state"}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_053b7a396de5"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_053b7a396de5">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_053b7a396de5">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_053b7a396de5/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_053b7a396de5/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_17fc441f5a27">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_17fc441f5a27" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="example_mystring">Example: <code>MyString</code></h2>
<div data-outline="example_mystring">
<p>Let's revisit <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-designing-an-abstract-type#example_different_representations_for_strings"><code>MyString</code></a>. Using an interface instead of a class for the ADT, we can support multiple implementations:</p><pre><code class="language-java hljs"><span class="hljs-comment handout-javadoc-comment">/** MyString represents an immutable sequence of characters. */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MyString</span> </span>{
<span class="hljs-comment">// We'll skip this creator operation for now</span>
<span class="hljs-comment">// /** @param b a boolean value</span>
<span class="hljs-comment">// * @return string representation of b, either "true" or "false" */</span>
<span class="hljs-comment">// public static MyString valueOf(boolean b) { ... }</span>
<span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@return</span> number of characters in this string */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">length</span><span class="hljs-params">()</span></span>;
<span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@param</span> i character position (requires 0 <= i < string length)
* <span class="hljs-doctag">@return</span> character at position i */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">char</span> <span class="hljs-title">charAt</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span></span>;
<span class="hljs-comment handout-javadoc-comment">/** Get the substring between start (inclusive) and end (exclusive).
* <span class="hljs-doctag">@param</span> start starting index
* <span class="hljs-doctag">@param</span> end ending index. Requires 0 <= start <= end <= string length.
* <span class="hljs-doctag">@return</span> string consisting of charAt(start)...charAt(end-1) */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> MyString <span class="hljs-title">substring</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span></span>;
}</code></pre>
<p>We'll skip the static <code>valueOf</code> method and come back to it in a minute. Instead, let's go ahead using a different technique from our <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-realizing-adt-concepts-in-java#realizing_adt_concepts_in_java">toolbox of ADT concepts in Java</a>: constructors.</p>
<p>Here's our first implementation:</p><pre><code class="language-java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SimpleMyString</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MyString</span> </span>{
<span class="hljs-keyword">private</span> <span class="hljs-keyword">char</span>[] a;
<span class="hljs-comment">/* Create an uninitialized SimpleMyString. */</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">SimpleMyString</span><span class="hljs-params">()</span> </span>{}
<span class="hljs-comment handout-javadoc-comment">/** Create a string representation of b, either "true" or "false".
* <span class="hljs-doctag">@param</span> b a boolean value */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">SimpleMyString</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> b)</span> </span>{
a = b ? <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[] { <span class="hljs-string">'t'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'e'</span> }
: <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[] { <span class="hljs-string">'f'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'e'</span> };
}
<span class="hljs-annotation">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">length</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> a.length; }
<span class="hljs-annotation">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">char</span> <span class="hljs-title">charAt</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span> </span>{ <span class="hljs-keyword">return</span> a[i]; }
<span class="hljs-annotation">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> MyString <span class="hljs-title">substring</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span> </span>{
SimpleMyString that = <span class="hljs-keyword">new</span> SimpleMyString();
that.a = <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[end - start];
System.arraycopy(<span class="hljs-keyword">this</span>.a, start, that.a, <span class="hljs-number">0</span>, end - start);
<span class="hljs-keyword">return</span> that;
}
}</code></pre>
<p>And here's the optimized implementation:</p><pre><code class="language-java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FastMyString</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MyString</span> </span>{
<span class="hljs-keyword">private</span> <span class="hljs-keyword">char</span>[] a;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> start;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> end;
<span class="hljs-comment">/* Create an uninitialized FastMyString. */</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">FastMyString</span><span class="hljs-params">()</span> </span>{}
<span class="hljs-comment handout-javadoc-comment">/** Create a string representation of b, either "true" or "false".
* <span class="hljs-doctag">@param</span> b a boolean value */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">FastMyString</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> b)</span> </span>{
a = b ? <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[] { <span class="hljs-string">'t'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'e'</span> }
: <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[] { <span class="hljs-string">'f'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'e'</span> };
start = <span class="hljs-number">0</span>;
end = a.length;
}
<span class="hljs-annotation">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">length</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> end - start; }
<span class="hljs-annotation">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">char</span> <span class="hljs-title">charAt</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span> </span>{ <span class="hljs-keyword">return</span> a[start + i]; }
<span class="hljs-annotation">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> MyString <span class="hljs-title">substring</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span> </span>{
FastMyString that = <span class="hljs-keyword">new</span> FastMyString();
that.a = <span class="hljs-keyword">this</span>.a;
that.start = <span class="hljs-keyword">this</span>.start + start;
that.end = <span class="hljs-keyword">this</span>.start + end;
<span class="hljs-keyword">return</span> that;
}
}</code></pre>
<ul>
<li>
<p>Compare these classes to the <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-designing-an-abstract-type#example_different_representations_for_strings">implementations of <code>MyString</code> in <em>Abstract Data Types</em></a>. Notice how the code that previously appeared in static <code>valueOf</code> methods now appears in the constructors, slightly changed to refer to the rep of <code>this</code>.</p>
</li>
<li>
<p>Also notice the use of <a href="https://docs.oracle.com/javase/tutorial/java/annotations/predefined.html"><code>@Override</code></a>. This annotation informs the compiler that the method must have the same signature as one of the methods in the interface we're implementing. But since the compiler already checks that we've implemented all of the interface methods, the primary value of <code>@Override</code> here is for readers of the code: it tells us to look for the spec of that method in the interface. Repeating the spec wouldn't be DRY, but saying nothing at all makes the code harder to understand.</p>
</li>
<li>
<p>And notice the private empty constructors we use to make new instances in <code>substring(..)</code> before we fill in their reps with data. We didn't have to write these empty constructors before because Java provides them by default when we don't declare any other constructors. Adding the constructors that take <code>boolean b</code> means we have to declare the other constructors explicitly.</p>
<p>Now that we know good ADTs scrupulously <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-invariants#invariants">preserve their own invariants</a>, these do-nothing constructors are a <strong>bad</strong> pattern: they don't assign any values to the rep, and they certainly don't establish any invariants. We should strongly consider revising the implementation. Since <code>MyString</code> is immutable, a starting point would be making all the fields <code>final</code>.</p>
</li>
</ul>
<p>How will clients use this ADT? Here's an example:</p><pre><code class="language-java hljs">MyString s = <span class="hljs-keyword">new</span> FastMyString(<span class="hljs-keyword">true</span>);
System.out.println(<span class="hljs-string">"The first character is: "</span> + s.charAt(<span class="hljs-number">0</span>));</code></pre>
<p>This code looks very similar to the code we write to use the Java collections classes:</p><pre><code class="language-java hljs">List<String> s = <span class="hljs-keyword">new</span> ArrayList<String>();
...</code></pre>
<p>Unfortunately, this pattern <strong>breaks the abstraction barrier</strong> we've worked so hard to build between the abstract type and its concrete representations. Clients must know the name of the concrete representation class. Because interfaces in Java cannot contain constructors, they must directly call one of the concrete class' constructors. The spec of that constructor won't appear anywhere in the interface, so there's no static guarantee that different implementations will even provide the same constructors.</p>
<p>Fortunately, (as of Java 8) interfaces <em>are</em> allowed to contain static methods, so we can implement the creator operation <code>valueOf</code> as a static factory method in the interface <code>MyString</code>:</p><pre><code class="language-java hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MyString</span> </span>{
<span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@param</span> b a boolean value
* <span class="hljs-doctag">@return</span> string representation of b, either "true" or "false" */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> MyString <span class="hljs-title">valueOf</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> b)</span> </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> FastMyString(<span class="hljs-keyword">true</span>);
}
<span class="hljs-comment">// ...</span></code></pre>
<p>Now a client can use the ADT without breaking the abstraction barrier:</p><pre><code class="language-java hljs">MyString s = MyString.valueOf(<span class="hljs-keyword">true</span>);
System.out.println(<span class="hljs-string">"The first character is: "</span> + s.charAt(<span class="hljs-number">0</span>));</code></pre>
</div>
<h2 id="example_set">Example: <code>Set</code></h2>
<div data-outline="example_set">
<p>Java's collection classes provide a good example of the idea of separating interface and implementation.</p>
<p>Let's consider as an example one of the ADTs from the Java collections library, <code>Set</code>.
<code>Set</code> is the ADT of finite sets of elements of some other type <code>E</code>. Here is a simplified version of the <code>Set</code> interface:</p><pre><code class="language-java hljs"><span class="hljs-comment handout-javadoc-comment">/** A mutable set.
* <span class="hljs-doctag">@param</span> <E> type of elements in the set */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Set</span><<span class="hljs-title">E</span>> </span>{</code></pre>
<p><code>Set</code> is an example of a <em>generic type</em>: a type whose specification is in terms of a placeholder type to be filled in later. Instead of writing separate specifications and implementations for <code>Set<String></code>, <code>Set<Integer></code>, and so on, we design and implement one <code>Set<E></code>.</p>
<p>We can match Java interfaces with our classification of ADT operations, starting with a creator:</p><pre><code class="language-java hljs"> <span class="hljs-comment">// example creator operation</span>
<span class="hljs-comment handout-javadoc-comment">/** Make an empty set.
* <span class="hljs-doctag">@param</span> <E> type of elements in the set
* <span class="hljs-doctag">@return</span> a new set instance, initially empty */</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <E> <span class="hljs-function">Set<E> <span class="hljs-title">make</span><span class="hljs-params">()</span> </span>{ ... } </code></pre>
<p>The <code>make</code> operation is implemented as a static factory method. Clients will write code like:
<br>
<code>Set<String> strings = Set.make();</code>
<br> and the compiler will understand that the new <code>Set</code> is a set of <code>String</code> objects.</p><pre><code class="language-java hljs"> <span class="hljs-comment">// example observer operations</span>
<span class="hljs-comment handout-javadoc-comment">/** Get size of the set.
* <span class="hljs-doctag">@return</span> the number of elements in this set */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">size</span><span class="hljs-params">()</span></span>;
<span class="hljs-comment handout-javadoc-comment">/** Test for membership.
* <span class="hljs-doctag">@param</span> e an element
* <span class="hljs-doctag">@return</span> true if and only if this set contains e */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">contains</span><span class="hljs-params">(E e)</span></span>;</code></pre>
<p>Next we have two observer methods. Notice how the specs are in terms of our abstract notion of a set; it would be malformed to mention the details of any particular implementation of sets with particular private fields. These specs should apply to any valid implementation of the set ADT.</p><pre><code class="language-java hljs"> <span class="hljs-comment">// example mutator operations</span>
<span class="hljs-comment handout-javadoc-comment">/** Modifies this set by adding e to the set.
* <span class="hljs-doctag">@param</span> e element to add */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(E e)</span></span>;
<span class="hljs-comment handout-javadoc-comment">/** Modifies this set by removing e, if found.
* If e is not found in the set, has no effect.
* <span class="hljs-doctag">@param</span> e element to remove */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">(E e)</span></span>;</code></pre>
<p>The story for these mutators is basically the same as for the observers. We still write specs at the level of our abstract model of sets.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_ed457a866952" data-graded="True">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-collection-interfaces-implementations-34">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-collection-interfaces-implementations-34" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-collection-interfaces-implementations-34" class="problems-wrapper" role="group"
aria-labelledby="11-collection-interfaces-implementations-34-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-collection-interfaces-implementations-34" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-collection-interfaces-implementations-34/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="5"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-collection-interfaces-implementations-34-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-collection-interfaces-implementations-34-problem-progress" tabindex="-1">
collection interfaces &amp; implementations
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-collection-interfaces-implementations-34-problem-progress"></div>
<div class="problem">
<div>
<p>Assume the following lines of code are run in sequence, and that any lines of code that don't compile are simply commented out so that the rest of the code can compile.</p>
<p>The code uses two methods from <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html" target="_new">java.util.Collections</a>, so you might need to consult the documentation.</p>
<p>Choose the <strong>most specific answer</strong> to each question.</p>
<pre class="code">
Set&lt;String&gt; set = new HashSet&lt;String&gt;();
</pre>
<p>The <code>set</code> variable now points to:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<select name="input_11-collection-interfaces-implementations-34_2_1" id="input_11-collection-interfaces-implementations-34_2_1" aria-describedby="status_11-collection-interfaces-implementations-34_2_1">
<option value="option_11-collection-interfaces-implementations-34_2_1_dummy_default">Select an option</option>
<option value="a HashSet object"> a HashSet object</option>
<option value="an object that implements the Set interface"> an object that implements the Set interface</option>
<option value="null"> null</option>
<option value="this line won't compile"> this line won't compile</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-collection-interfaces-implementations-34_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-collection-interfaces-implementations-34_2_1"/>
</div></div>
<div class="solution-span">
<span id="solution_11-collection-interfaces-implementations-34_solution_1"/>
</div><pre class="code">
set = Collections.unmodifiableSet(set);
</pre>
<p>The <code>set</code> variable now points to:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<select name="input_11-collection-interfaces-implementations-34_3_1" id="input_11-collection-interfaces-implementations-34_3_1" aria-describedby="status_11-collection-interfaces-implementations-34_3_1">
<option value="option_11-collection-interfaces-implementations-34_3_1_dummy_default">Select an option</option>
<option value="a HashSet object"> a HashSet object</option>
<option value="an object that implements the Set interface"> an object that implements the Set interface</option>
<option value="null"> null</option>
<option value="this line won't compile"> this line won't compile</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-collection-interfaces-implementations-34_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-collection-interfaces-implementations-34_3_1"/>
</div></div>
<div class="solution-span">
<span id="solution_11-collection-interfaces-implementations-34_solution_2"/>
</div><pre class="code">
set = Collections.singleton("glorp");
</pre>
<p>The <code>set</code> variable now points to:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<select name="input_11-collection-interfaces-implementations-34_4_1" id="input_11-collection-interfaces-implementations-34_4_1" aria-describedby="status_11-collection-interfaces-implementations-34_4_1">
<option value="option_11-collection-interfaces-implementations-34_4_1_dummy_default">Select an option</option>
<option value="a HashSet object"> a HashSet object</option>
<option value="an object that implements the Set interface"> an object that implements the Set interface</option>
<option value="null"> null</option>
<option value="this line won't compile"> this line won't compile</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-collection-interfaces-implementations-34_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-collection-interfaces-implementations-34_4_1"/>
</div></div>
<div class="solution-span">
<span id="solution_11-collection-interfaces-implementations-34_solution_3"/>
</div><pre class="code">
set = new Set&lt;String&gt;();
</pre>
<p>The <code>set</code> variable now points to:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="inputtype option-input ">
<select name="input_11-collection-interfaces-implementations-34_5_1" id="input_11-collection-interfaces-implementations-34_5_1" aria-describedby="status_11-collection-interfaces-implementations-34_5_1">
<option value="option_11-collection-interfaces-implementations-34_5_1_dummy_default">Select an option</option>
<option value="a HashSet object"> a HashSet object</option>
<option value="an object that implements the Set interface"> an object that implements the Set interface</option>
<option value="null"> null</option>
<option value="this line won't compile"> this line won't compile</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-collection-interfaces-implementations-34_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-collection-interfaces-implementations-34_5_1"/>
</div></div>
<div class="solution-span">
<span id="solution_11-collection-interfaces-implementations-34_solution_4"/>
</div><pre class="code">
List&lt;String&gt; list = set;
</pre>
<p>The <code>set</code> variable now points to:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 5" role="group"><div class="inputtype option-input ">
<select name="input_11-collection-interfaces-implementations-34_6_1" id="input_11-collection-interfaces-implementations-34_6_1" aria-describedby="status_11-collection-interfaces-implementations-34_6_1">
<option value="option_11-collection-interfaces-implementations-34_6_1_dummy_default">Select an option</option>
<option value="a HashSet object"> a HashSet object</option>
<option value="an object that implements the Set interface"> an object that implements the Set interface</option>
<option value="null"> null</option>
<option value="this line won't compile"> this line won't compile</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_11-collection-interfaces-implementations-34_6_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_11-collection-interfaces-implementations-34_6_1"/>
</div></div>
<div class="solution-span">
<span id="solution_11-collection-interfaces-implementations-34_solution_5"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="collection interfaces &amp; implementations" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-collection-interfaces-implementations-34" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-collection-interfaces-implementations-34">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-collection-interfaces-implementations-34-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-collection-interfaces-implementations-34-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-collection-interfaces-implementations-34-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Why_Interfaces_" data-graded="True">
<h2 class="hd hd-2 unit-title">Why Interfaces?</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_87c6a3fac3d1">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-block-type="video" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_87c6a3fac3d1" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_87c6a3fac3d1"
class="video closed"
data-metadata='{"completionEnabled": false, "start": 0.0, "saveStateEnabled": false, "lmsRootURL": "https://openlearninglibrary.mit.edu", "showCaptions": "true", "captionDataDir": null, "poster": null, "streams": "1.00:SxmwGJbZowc", "ytApiUrl": "https://www.youtube.com/iframe_api", "transcriptLanguages": {"en": "English"}, "duration": 275.69, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V006300_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V006300/MIT600512016-V006300.m3u8"], "recordedYoutubeIsAvailable": true, "prioritizeHls": false, "autoplay": false, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_87c6a3fac3d1/handler/transcript/available_translations", "autoAdvance": false, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_87c6a3fac3d1/handler/transcript/translation/__lang__", "generalSpeed": 1.0, "savedVideoPosition": 0.0, "completionPercentage": 0.95, "speed": null, "autohideHtml5": false, "end": 0.0, "ytMetadataEndpoint": "", "transcriptLanguage": "en", "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_87c6a3fac3d1/handler/publish_completion", "ytTestTimeout": 1500, "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_87c6a3fac3d1/handler/xmodule_handler/save_user_state"}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_87c6a3fac3d1"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_87c6a3fac3d1">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_87c6a3fac3d1">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_87c6a3fac3d1/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_87c6a3fac3d1/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_10cbf2eff77d">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_10cbf2eff77d" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="why_interfaces">Why Interfaces?</h2>
<div data-outline="why_interfaces">
<p>Interfaces are used pervasively in real Java code. Not every class is associated with an interface, but there are a few good reasons to bring an interface into the picture.</p>
<ul>
<li><strong>Documentation for both the compiler and for humans</strong>. Not only does an interface help the compiler catch ADT implementation bugs, but it is also much more useful for a human to read than the code for a concrete implementation. Such an implementation intersperses ADT-level types and specs with implementation details.</li>
<li><strong>Allowing performance trade-offs</strong>. Different implementations of the ADT can provide methods with very different performance characteristics. Different applications may work better with different choices, but we would like to code these applications in a way that is representation-independent. From a correctness standpoint, it should be possible to drop in any new implementation of a key ADT with simple, localized code changes.</li>
<li><strong>Optional methods</strong>. <code>List</code> from the Java standard library marks all mutator methods as optional. By building an implementation that does not support these methods, we can provide immutable lists. Some operations are hard to implement with good enough performance on immutable lists, so we want mutable implementations, too. Code that doesn't call mutators can be written to work automatically with either kind of list.</li>
<li><strong>Methods with intentionally underdetermined specifications</strong>. An ADT for finite sets could leave unspecified the element order one gets when converting to a list. Some implementations might use slower method implementations that manage to keep the set representation in some sorted order, allowing quick conversion to a sorted list. Other implementations might make many methods faster by not bothering to support conversion to sorted lists.</li>
<li><strong>Multiple views of one class</strong>. A Java class may implement multiple interfaces. For instance, a user interface widget displaying a drop-down list is natural to view as both a widget and a list. The class for this widget could implement both interfaces. In other words, we don't implement an ADT multiple times just because we are choosing different data structures; we may make multiple implementations because many different sorts of objects may also be seen as special cases of the ADT, among other useful perspectives.</li>
<li><strong>More and less trustworthy implementations</strong>. Another reason to implement an interface multiple times might be that it is easy to build a simple implementation that you believe is correct, while you can work harder to build a fancier version that is more likely to contain bugs. You can choose implementations for applications based on how bad it would be to get bitten by a bug.</li>
</ul>
</div>
<h2 id="realizing_adt_concepts_in_java">Realizing ADT Concepts in Java</h2>
<div data-outline="realizing_adt_concepts_in_java">
<p>We can now extend our <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-realizing-adt-concepts-in-java#realizing_adt_concepts_in_java">Java toolbox of ADT concepts</a> from the first ADTs reading:</p>
<table class="adts-in-java-table">
<tbody>
<tr>
<th>
<p>ADT concept</p>
</th>
<th>
<p>Ways to do it in Java</p>
</th>
<th>
<p>Examples</p>
</th>
</tr>
<tr class="new-row">
<td>
<p>Abstract data type</p>
</td>
<td>
<p>Single class</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html"><code>String</code></a></p>
</td>
</tr>
<tr class="yellow-highlight">
<td></td>
<td>
<p>Interface + class(es)</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/List.html"><code>List</code></a> and <a href="http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html"><code>ArrayList</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Creator operation</p>
</td>
<td>
<p>Constructor</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList--"><code>ArrayList()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Static (factory) method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#singletonList-T-"><code>Collections.singletonList()</code></a>, <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-"><code>Arrays.toList()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Constant</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#ZERO"><code>BigInteger.ZERO</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Observer operation</p>
</td>
<td>
<p>Instance method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/List.html#get-int-"><code>List.get()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Static method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#max-java.util.Collection-"><code>Collections.max()</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Producer operation</p>
</td>
<td>
<p>Instance method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#trim--"><code>String.trim()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Static method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-"><code>Collections.unmodifiableList()</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Mutator operation</p>
</td>
<td>
<p>Instance method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/List.html#add-E-"><code>List.add()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Static method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#copy-java.util.List-java.util.List-"><code>Collections.copy()</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Representation</p>
</td>
<td>
<p><code>private</code> fields</p>
</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_cfcd04c34272" data-graded="True">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-using-interfaces-for-adts" class="problems-wrapper" role="group"
aria-labelledby="11-using-interfaces-for-adts-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-using-interfaces-for-adts-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-problem-progress" tabindex="-1">
using interfaces for ADTs
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-problem-progress"></div>
<div class="problem">
<div>
<p>Suppose you have an abstract data type for rational numbers, which is currently represented as a Java class:</p>
<pre class="code">
public class RatNum {
...
}
</pre>
<p>You decide to change <code>RatNum</code> to a Java interface instead, along with an implementation class called <code>IntFraction</code>:</p>
<pre class="code">
public interface RatNum {
...
}
public class IntFraction implements RatNum {
...
}
</pre>
<p>For each piece of code below from the old RatNum class, identify it and decide where it should go in the new interface/implementation class design.</p>
<pre class="code">
private int numer;
private int denom;
</pre>
<p>This piece of code is: (check all that apply)</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts_2_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts_2_1">
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts_2_1[]" id="input_11-using-interfaces-for-adts_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="11-using-interfaces-for-adts_2_1-choice_0-label" for="input_11-using-interfaces-for-adts_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_2_1"> abstraction function
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts_2_1[]" id="input_11-using-interfaces-for-adts_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="11-using-interfaces-for-adts_2_1-choice_1-label" for="input_11-using-interfaces-for-adts_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_2_1"> creator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts_2_1[]" id="input_11-using-interfaces-for-adts_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="11-using-interfaces-for-adts_2_1-choice_2-label" for="input_11-using-interfaces-for-adts_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_2_1"> mutator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts_2_1[]" id="input_11-using-interfaces-for-adts_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="11-using-interfaces-for-adts_2_1-choice_3-label" for="input_11-using-interfaces-for-adts_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_2_1"> observer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts_2_1[]" id="input_11-using-interfaces-for-adts_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="11-using-interfaces-for-adts_2_1-choice_4-label" for="input_11-using-interfaces-for-adts_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_2_1"> producer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts_2_1[]" id="input_11-using-interfaces-for-adts_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="11-using-interfaces-for-adts_2_1-choice_5-label" for="input_11-using-interfaces-for-adts_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_2_1"> rep
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts_2_1[]" id="input_11-using-interfaces-for-adts_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="11-using-interfaces-for-adts_2_1-choice_6-label" for="input_11-using-interfaces-for-adts_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_2_1"> rep invariant
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts_2_1[]" id="input_11-using-interfaces-for-adts_2_1_choice_7" class="field-input input-checkbox" value="choice_7"/><label id="11-using-interfaces-for-adts_2_1-choice_7-label" for="input_11-using-interfaces-for-adts_2_1_choice_7" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_2_1"> specification
</label>
</div>
<span id="answer_11-using-interfaces-for-adts_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>It should be put in:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts_3_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts_3_1">
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts_3_1" id="input_11-using-interfaces-for-adts_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="11-using-interfaces-for-adts_3_1-choice_0-label" for="input_11-using-interfaces-for-adts_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_3_1"> the interface
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts_3_1" id="input_11-using-interfaces-for-adts_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="11-using-interfaces-for-adts_3_1-choice_1-label" for="input_11-using-interfaces-for-adts_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_3_1"> the implementation class
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts_3_1" id="input_11-using-interfaces-for-adts_3_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="11-using-interfaces-for-adts_3_1-choice_2-label" for="input_11-using-interfaces-for-adts_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts_3_1"> both
</label>
</div>
<span id="answer_11-using-interfaces-for-adts_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_11-using-interfaces-for-adts_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="using interfaces for ADTs" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-using-interfaces-for-adts" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-using-interfaces-for-adts">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-2">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-2" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-using-interfaces-for-adts-2" class="problems-wrapper" role="group"
aria-labelledby="11-using-interfaces-for-adts-2-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-2" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-2/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-using-interfaces-for-adts-2-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-2-problem-progress" tabindex="-1">
using interfaces for ADTs, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-2-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
// denom &gt; 0
// numer/denom is in reduced form
</pre>
<p>This piece of code is: (check all that apply)</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-2_2_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-2_2_1">
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-2_2_1[]" id="input_11-using-interfaces-for-adts-2_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="11-using-interfaces-for-adts-2_2_1-choice_0-label" for="input_11-using-interfaces-for-adts-2_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_2_1"> abstraction function
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-2_2_1[]" id="input_11-using-interfaces-for-adts-2_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="11-using-interfaces-for-adts-2_2_1-choice_1-label" for="input_11-using-interfaces-for-adts-2_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_2_1"> creator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-2_2_1[]" id="input_11-using-interfaces-for-adts-2_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="11-using-interfaces-for-adts-2_2_1-choice_2-label" for="input_11-using-interfaces-for-adts-2_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_2_1"> mutator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-2_2_1[]" id="input_11-using-interfaces-for-adts-2_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="11-using-interfaces-for-adts-2_2_1-choice_3-label" for="input_11-using-interfaces-for-adts-2_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_2_1"> observer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-2_2_1[]" id="input_11-using-interfaces-for-adts-2_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="11-using-interfaces-for-adts-2_2_1-choice_4-label" for="input_11-using-interfaces-for-adts-2_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_2_1"> producer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-2_2_1[]" id="input_11-using-interfaces-for-adts-2_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="11-using-interfaces-for-adts-2_2_1-choice_5-label" for="input_11-using-interfaces-for-adts-2_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_2_1"> rep
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-2_2_1[]" id="input_11-using-interfaces-for-adts-2_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="11-using-interfaces-for-adts-2_2_1-choice_6-label" for="input_11-using-interfaces-for-adts-2_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_2_1"> rep invariant
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-2_2_1[]" id="input_11-using-interfaces-for-adts-2_2_1_choice_7" class="field-input input-checkbox" value="choice_7"/><label id="11-using-interfaces-for-adts-2_2_1-choice_7-label" for="input_11-using-interfaces-for-adts-2_2_1_choice_7" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_2_1"> specification
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-2_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-2_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>It should be put in:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-2_3_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-2_3_1">
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-2_3_1" id="input_11-using-interfaces-for-adts-2_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="11-using-interfaces-for-adts-2_3_1-choice_0-label" for="input_11-using-interfaces-for-adts-2_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_3_1"> the interface
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-2_3_1" id="input_11-using-interfaces-for-adts-2_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="11-using-interfaces-for-adts-2_3_1-choice_1-label" for="input_11-using-interfaces-for-adts-2_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_3_1"> the implementation class
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-2_3_1" id="input_11-using-interfaces-for-adts-2_3_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="11-using-interfaces-for-adts-2_3_1-choice_2-label" for="input_11-using-interfaces-for-adts-2_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-2_3_1"> both
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-2_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-2_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_11-using-interfaces-for-adts-2_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="using interfaces for ADTs, part 2" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-using-interfaces-for-adts-2" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-using-interfaces-for-adts-2">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-2-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-2-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-2-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-3" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-3">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-3" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-using-interfaces-for-adts-3" class="problems-wrapper" role="group"
aria-labelledby="11-using-interfaces-for-adts-3-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-3" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-3/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-using-interfaces-for-adts-3-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-3-problem-progress" tabindex="-1">
using interfaces for ADTs, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-3-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
// represents the rational number numer / denom
</pre>
<p>This piece of code is: (check all that apply)</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-3_2_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-3_2_1">
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-3_2_1[]" id="input_11-using-interfaces-for-adts-3_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="11-using-interfaces-for-adts-3_2_1-choice_0-label" for="input_11-using-interfaces-for-adts-3_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_2_1"> abstraction function
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-3_2_1[]" id="input_11-using-interfaces-for-adts-3_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="11-using-interfaces-for-adts-3_2_1-choice_1-label" for="input_11-using-interfaces-for-adts-3_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_2_1"> creator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-3_2_1[]" id="input_11-using-interfaces-for-adts-3_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="11-using-interfaces-for-adts-3_2_1-choice_2-label" for="input_11-using-interfaces-for-adts-3_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_2_1"> mutator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-3_2_1[]" id="input_11-using-interfaces-for-adts-3_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="11-using-interfaces-for-adts-3_2_1-choice_3-label" for="input_11-using-interfaces-for-adts-3_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_2_1"> observer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-3_2_1[]" id="input_11-using-interfaces-for-adts-3_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="11-using-interfaces-for-adts-3_2_1-choice_4-label" for="input_11-using-interfaces-for-adts-3_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_2_1"> producer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-3_2_1[]" id="input_11-using-interfaces-for-adts-3_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="11-using-interfaces-for-adts-3_2_1-choice_5-label" for="input_11-using-interfaces-for-adts-3_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_2_1"> rep
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-3_2_1[]" id="input_11-using-interfaces-for-adts-3_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="11-using-interfaces-for-adts-3_2_1-choice_6-label" for="input_11-using-interfaces-for-adts-3_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_2_1"> rep invariant
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-3_2_1[]" id="input_11-using-interfaces-for-adts-3_2_1_choice_7" class="field-input input-checkbox" value="choice_7"/><label id="11-using-interfaces-for-adts-3_2_1-choice_7-label" for="input_11-using-interfaces-for-adts-3_2_1_choice_7" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_2_1"> specification
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-3_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-3_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>It should be put in:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-3_3_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-3_3_1">
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-3_3_1" id="input_11-using-interfaces-for-adts-3_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="11-using-interfaces-for-adts-3_3_1-choice_0-label" for="input_11-using-interfaces-for-adts-3_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_3_1"> the interface
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-3_3_1" id="input_11-using-interfaces-for-adts-3_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="11-using-interfaces-for-adts-3_3_1-choice_1-label" for="input_11-using-interfaces-for-adts-3_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_3_1"> the implementation class
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-3_3_1" id="input_11-using-interfaces-for-adts-3_3_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="11-using-interfaces-for-adts-3_3_1-choice_2-label" for="input_11-using-interfaces-for-adts-3_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-3_3_1"> both
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-3_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-3_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_11-using-interfaces-for-adts-3_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="using interfaces for ADTs, part 3" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-using-interfaces-for-adts-3" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-using-interfaces-for-adts-3">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-3-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-3-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-3-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-4" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-4">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-4" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-using-interfaces-for-adts-4" class="problems-wrapper" role="group"
aria-labelledby="11-using-interfaces-for-adts-4-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-4" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-4/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-using-interfaces-for-adts-4-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-4-problem-progress" tabindex="-1">
using interfaces for ADTs, part 4
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-4-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
/**
* @param that another RatNum
* @return a RatNum equal to (this / that)
*/
</pre>
<p>This piece of code is: (check all that apply)</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-4_2_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-4_2_1">
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-4_2_1[]" id="input_11-using-interfaces-for-adts-4_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="11-using-interfaces-for-adts-4_2_1-choice_0-label" for="input_11-using-interfaces-for-adts-4_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_2_1"> abstraction function
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-4_2_1[]" id="input_11-using-interfaces-for-adts-4_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="11-using-interfaces-for-adts-4_2_1-choice_1-label" for="input_11-using-interfaces-for-adts-4_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_2_1"> creator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-4_2_1[]" id="input_11-using-interfaces-for-adts-4_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="11-using-interfaces-for-adts-4_2_1-choice_2-label" for="input_11-using-interfaces-for-adts-4_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_2_1"> mutator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-4_2_1[]" id="input_11-using-interfaces-for-adts-4_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="11-using-interfaces-for-adts-4_2_1-choice_3-label" for="input_11-using-interfaces-for-adts-4_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_2_1"> observer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-4_2_1[]" id="input_11-using-interfaces-for-adts-4_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="11-using-interfaces-for-adts-4_2_1-choice_4-label" for="input_11-using-interfaces-for-adts-4_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_2_1"> producer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-4_2_1[]" id="input_11-using-interfaces-for-adts-4_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="11-using-interfaces-for-adts-4_2_1-choice_5-label" for="input_11-using-interfaces-for-adts-4_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_2_1"> rep
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-4_2_1[]" id="input_11-using-interfaces-for-adts-4_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="11-using-interfaces-for-adts-4_2_1-choice_6-label" for="input_11-using-interfaces-for-adts-4_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_2_1"> rep invariant
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-4_2_1[]" id="input_11-using-interfaces-for-adts-4_2_1_choice_7" class="field-input input-checkbox" value="choice_7"/><label id="11-using-interfaces-for-adts-4_2_1-choice_7-label" for="input_11-using-interfaces-for-adts-4_2_1_choice_7" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_2_1"> specification
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-4_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-4_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>It should be put in:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-4_3_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-4_3_1">
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-4_3_1" id="input_11-using-interfaces-for-adts-4_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="11-using-interfaces-for-adts-4_3_1-choice_0-label" for="input_11-using-interfaces-for-adts-4_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_3_1"> the interface
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-4_3_1" id="input_11-using-interfaces-for-adts-4_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="11-using-interfaces-for-adts-4_3_1-choice_1-label" for="input_11-using-interfaces-for-adts-4_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_3_1"> the implementation class
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-4_3_1" id="input_11-using-interfaces-for-adts-4_3_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="11-using-interfaces-for-adts-4_3_1-choice_2-label" for="input_11-using-interfaces-for-adts-4_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-4_3_1"> both
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-4_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-4_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_11-using-interfaces-for-adts-4_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="using interfaces for ADTs, part 4" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-using-interfaces-for-adts-4" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-using-interfaces-for-adts-4">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-4-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-4-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-4-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-5" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-5">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-5" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-using-interfaces-for-adts-5" class="problems-wrapper" role="group"
aria-labelledby="11-using-interfaces-for-adts-5-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-5" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-5/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-using-interfaces-for-adts-5-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-5-problem-progress" tabindex="-1">
using interfaces for ADTs, part 5
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-5-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
public boolean isZero()
</pre>
<p>This piece of code is: (check all that apply)</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-5_2_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-5_2_1">
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-5_2_1[]" id="input_11-using-interfaces-for-adts-5_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="11-using-interfaces-for-adts-5_2_1-choice_0-label" for="input_11-using-interfaces-for-adts-5_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_2_1"> abstraction function
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-5_2_1[]" id="input_11-using-interfaces-for-adts-5_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="11-using-interfaces-for-adts-5_2_1-choice_1-label" for="input_11-using-interfaces-for-adts-5_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_2_1"> creator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-5_2_1[]" id="input_11-using-interfaces-for-adts-5_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="11-using-interfaces-for-adts-5_2_1-choice_2-label" for="input_11-using-interfaces-for-adts-5_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_2_1"> mutator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-5_2_1[]" id="input_11-using-interfaces-for-adts-5_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="11-using-interfaces-for-adts-5_2_1-choice_3-label" for="input_11-using-interfaces-for-adts-5_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_2_1"> observer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-5_2_1[]" id="input_11-using-interfaces-for-adts-5_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="11-using-interfaces-for-adts-5_2_1-choice_4-label" for="input_11-using-interfaces-for-adts-5_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_2_1"> producer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-5_2_1[]" id="input_11-using-interfaces-for-adts-5_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="11-using-interfaces-for-adts-5_2_1-choice_5-label" for="input_11-using-interfaces-for-adts-5_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_2_1"> rep
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-5_2_1[]" id="input_11-using-interfaces-for-adts-5_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="11-using-interfaces-for-adts-5_2_1-choice_6-label" for="input_11-using-interfaces-for-adts-5_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_2_1"> rep invariant
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-5_2_1[]" id="input_11-using-interfaces-for-adts-5_2_1_choice_7" class="field-input input-checkbox" value="choice_7"/><label id="11-using-interfaces-for-adts-5_2_1-choice_7-label" for="input_11-using-interfaces-for-adts-5_2_1_choice_7" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_2_1"> specification
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-5_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-5_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>It should be put in:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-5_3_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-5_3_1">
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-5_3_1" id="input_11-using-interfaces-for-adts-5_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="11-using-interfaces-for-adts-5_3_1-choice_0-label" for="input_11-using-interfaces-for-adts-5_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_3_1"> the interface
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-5_3_1" id="input_11-using-interfaces-for-adts-5_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="11-using-interfaces-for-adts-5_3_1-choice_1-label" for="input_11-using-interfaces-for-adts-5_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_3_1"> the implementation class
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-5_3_1" id="input_11-using-interfaces-for-adts-5_3_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="11-using-interfaces-for-adts-5_3_1-choice_2-label" for="input_11-using-interfaces-for-adts-5_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-5_3_1"> both
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-5_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-5_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_11-using-interfaces-for-adts-5_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="using interfaces for ADTs, part 5" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-using-interfaces-for-adts-5" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-using-interfaces-for-adts-5">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-5-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-5-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-5-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-6" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-6">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-block-type="problem" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="True" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-6" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_11-using-interfaces-for-adts-6" class="problems-wrapper" role="group"
aria-labelledby="11-using-interfaces-for-adts-6-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-6" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-6/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="11-using-interfaces-for-adts-6-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-6-problem-progress" tabindex="-1">
using interfaces for ADTs, part 6
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@11-using-interfaces-for-adts-6-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
return numer == 0;
</pre>
<p>This piece of code is: (check all that apply)</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-6_2_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-6_2_1">
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-6_2_1[]" id="input_11-using-interfaces-for-adts-6_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="11-using-interfaces-for-adts-6_2_1-choice_0-label" for="input_11-using-interfaces-for-adts-6_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_2_1"> abstraction function
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-6_2_1[]" id="input_11-using-interfaces-for-adts-6_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="11-using-interfaces-for-adts-6_2_1-choice_1-label" for="input_11-using-interfaces-for-adts-6_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_2_1"> creator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-6_2_1[]" id="input_11-using-interfaces-for-adts-6_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="11-using-interfaces-for-adts-6_2_1-choice_2-label" for="input_11-using-interfaces-for-adts-6_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_2_1"> mutator
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-6_2_1[]" id="input_11-using-interfaces-for-adts-6_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="11-using-interfaces-for-adts-6_2_1-choice_3-label" for="input_11-using-interfaces-for-adts-6_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_2_1"> observer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-6_2_1[]" id="input_11-using-interfaces-for-adts-6_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="11-using-interfaces-for-adts-6_2_1-choice_4-label" for="input_11-using-interfaces-for-adts-6_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_2_1"> producer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-6_2_1[]" id="input_11-using-interfaces-for-adts-6_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="11-using-interfaces-for-adts-6_2_1-choice_5-label" for="input_11-using-interfaces-for-adts-6_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_2_1"> rep
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-6_2_1[]" id="input_11-using-interfaces-for-adts-6_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="11-using-interfaces-for-adts-6_2_1-choice_6-label" for="input_11-using-interfaces-for-adts-6_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_2_1"> rep invariant
</label>
</div>
<div class="field">
<input type="checkbox" name="input_11-using-interfaces-for-adts-6_2_1[]" id="input_11-using-interfaces-for-adts-6_2_1_choice_7" class="field-input input-checkbox" value="choice_7"/><label id="11-using-interfaces-for-adts-6_2_1-choice_7-label" for="input_11-using-interfaces-for-adts-6_2_1_choice_7" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_2_1"> specification
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-6_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-6_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>It should be put in:</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_11-using-interfaces-for-adts-6_3_1">
<fieldset aria-describedby="status_11-using-interfaces-for-adts-6_3_1">
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-6_3_1" id="input_11-using-interfaces-for-adts-6_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="11-using-interfaces-for-adts-6_3_1-choice_0-label" for="input_11-using-interfaces-for-adts-6_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_3_1"> the interface
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-6_3_1" id="input_11-using-interfaces-for-adts-6_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="11-using-interfaces-for-adts-6_3_1-choice_1-label" for="input_11-using-interfaces-for-adts-6_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_3_1"> the implementation class
</label>
</div>
<div class="field">
<input type="radio" name="input_11-using-interfaces-for-adts-6_3_1" id="input_11-using-interfaces-for-adts-6_3_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="11-using-interfaces-for-adts-6_3_1-choice_2-label" for="input_11-using-interfaces-for-adts-6_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_11-using-interfaces-for-adts-6_3_1"> both
</label>
</div>
<span id="answer_11-using-interfaces-for-adts-6_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_11-using-interfaces-for-adts-6_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_11-using-interfaces-for-adts-6_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="using interfaces for ADTs, part 6" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_11-using-interfaces-for-adts-6" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_11-using-interfaces-for-adts-6">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-6-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-6-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="11-using-interfaces-for-adts-6-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="VerticalStudentView" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Reading_11_Summary" data-graded="True">
<h2 class="hd hd-2 unit-title">Reading 11 Summary</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_441774ec704b">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-runtime-version="1" data-has-score="False" data-request-token="c3b85c2a4c3811ef857c16fffddbd97f" data-init="XBlockToXModuleShim" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_441774ec704b" data-graded="True">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="summary">Summary</h2>
<div data-outline="summary">
<p>Java interfaces help us formalize the idea of an abstract data type as a set of operations that must be supported by a type.</p>
<p>This helps make our code…</p>
<ul>
<li>
<p><strong>Safe from bugs.</strong> An ADT is defined by its operations, and interfaces do just that. When clients use an interface type, static checking ensures that they only use methods defined by the interface. If the implementation class exposes other methods — or worse, has visible representation — the client can't accidentally see or depend on them. When we have multiple implementations of a data type, interfaces provide static checking of the method signatures.</p>
</li>
<li>
<p><strong>Easy to understand.</strong> Clients and maintainers know exactly where to look for the specification of the ADT. Since the interface doesn't contain instance fields or implementations of instance methods, it's easier to keep details of the implementation out of the specifications.</p>
</li>
<li>
<p><strong>Ready for change.</strong> We can easily add new implementations of a type by adding classes that implement interface. If we avoid constructors in favor of static factory methods, clients will only see the interface. That means we can switch which implementation class clients are using without changing their code at all.</p>
</li>
</ul>
</div>
<div class="license">This reading was collaboratively authored with contributions from: Saman Amarasinghe, Adam Chlipala, Srini Devadas, Michael Ernst, Max Goldman, John Guttag, Daniel Jackson, Rob Miller, Martin Rinard, and Armando Solar-Lezama. This work is licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>.</div>
</div>
</div>
</div>
</div>