{"id":389,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=389"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"What Is an Interface?","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=389","title":{"rendered":"What Is an Interface?"},"content":{"rendered":"<p><DIV><br \/>\n<DIV id=PageTitle>What Is an Interface?<\/DIV><br \/>\n<BLOCKQUOTE>As you&#8217;ve already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object&#8217;s <I>interface<\/I> with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the &#8220;power&#8221; button to turn the television on and off.<br \/>\n<P>In its most common form, an interface is a group of related methods with empty bodies. A bicycle&#8217;s behavior, if specified as an interface, might appear as follows: <\/P><br \/>\n<BLOCKQUOTE><PRE>interface Bicycle {<\/p>\n<p>       void changeCadence(int newValue);<\/p>\n<p>       void changeGear(int newValue);<\/p>\n<p>       void speedUp(int increment);<\/p>\n<p>       void applyBrakes(int decrement);<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>To implement this interface, the name of your class would change (to <CODE>ACMEBicycle<\/CODE>, for example), and you&#8217;d use the <CODE>implements<\/CODE> keyword in the class declaration:<br \/>\n<BLOCKQUOTE><PRE>class ACMEBicycle <STRONG>implements<\/STRONG> Bicycle {<\/p>\n<p>   \/\/ remainder of this class implemented as before<\/p>\n<p>}<br \/>\n<\/PRE><\/BLOCKQUOTE>Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.<br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG>Note:<\/STRONG>&nbsp; To actually compile the <CODE>ACMEBicycle<\/CODE> class, you&#8217;ll need to add the <CODE>public<\/CODE> keyword to the beginning of the implemented interface methods. You&#8217;ll learn the reasons for this later in the lessons on <A class=TutorialLink href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/javaOO\/index.html\" target=_top>Classes and Objects<\/A> and <A class=TutorialLink href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/IandI\/index.html\" target=_top>Interfaces and Inheritance<\/A>.<\/BLOCKQUOTE><\/BLOCKQUOTE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What Is an Interface? As you&#8217;ve already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object&#8217;s interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side &hellip; <a href=\"https:\/\/www.strongd.net\/?p=389\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">What Is an Interface?<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-389","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=389"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/389\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}