{"id":3447,"date":"2019-06-20T11:43:24","date_gmt":"2019-06-20T11:43:24","guid":{"rendered":"http:\/\/softlect.in\/?p=3447"},"modified":"2019-06-20T12:01:28","modified_gmt":"2019-06-20T12:01:28","slug":"java-interfaces","status":"publish","type":"post","link":"http:\/\/softlect.com\/index.php\/java-interfaces\/","title":{"rendered":"Java Interfaces"},"content":{"rendered":"<h1><a name=\"_Toc179309431\"><\/a><strong>Interfaces<\/strong><\/h1>\n<p>&nbsp;<\/p>\n<p>Using the keyword <strong>interface<\/strong>, you can fully abstract a class\u2019 interface from its implementation. That is, using <strong>interface<\/strong>, you can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. Once it is defined, any number of classes can implement an <strong>interface<\/strong>. Also, one class can implement any number of interfaces. To implement an interface, a class must create the complete set of methods defined by the interface. How ever, each class is free to determine the details of its own implementation. By providing the <strong>interface <\/strong>keyword, Java allows you to fully utilize the \u201cone interface, multiple methods\u201d aspect of polymorphism. Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible.<\/p>\n<h2><a name=\"_Toc179309432\"><\/a>Defining an Interface<\/h2>\n<p>An interface is defined much like a class. This is the general form of an interface:<\/p>\n<p>&nbsp;<\/p>\n<p><em>access <\/em>interface <em>name <\/em>{<\/p>\n<p><em>return-type method-name1<\/em>(<em>parameter-list<\/em>)<em>;<\/em><\/p>\n<p><em>return-type method-name2<\/em>(<em>parameter-list<\/em>)<em>;<\/em><\/p>\n<p><em>type final-varname1 = value;<\/em><\/p>\n<p><em>type final-varname2 = value;<\/em><\/p>\n<p><em>\/\/ &#8230;<\/em><\/p>\n<p><em>return-type method-nameN<\/em>(<em>parameter-list<\/em>)<em>;<\/em><\/p>\n<p><em>type final-varnameN = value;<\/em><\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <em>access <\/em>is either <strong>public <\/strong>or not used. When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared. When it is declared as <strong>public<\/strong>, the interface can be used by any other code. <em>name <\/em>is the name of the interface, and can be any valid identifier. Notice that the methods which are declared have no bodies. They end with a semicolon after the parameter list. They are, essentially, abstract methods; there can be no default implementation of any method specified within an interface. Each class that includes an interface must implement all of the methods. Variables can be declared inside of interface declarations. They are implicitly <strong>final <\/strong>and <strong>static<\/strong>, meaning they cannot be changed by the implementing class. They must also be initialized with a constant value. All methods and variables are implicitly <strong>public <\/strong>if the interface, itself, is declared as <strong>public<\/strong>. Here is an example of an interface definition. It declares a simple interface which contains one method called <strong>callback( ) <\/strong>that takes a single integer parameter.<\/p>\n<p>&nbsp;<\/p>\n<p>interface Callback {<\/p>\n<p>void callback(int param);<\/p>\n<p>}<\/p>\n<h2><a name=\"_Toc179309433\"><\/a>Implementing Interfaces<\/h2>\n<p>Once an <strong>interface <\/strong>has been defined, one or more classes can implement that interface. To implement an interface, include the <strong>implements <\/strong>clause in a class definition, and then create the methods defined by the interface. The general form of a class that includes the <strong>implements <\/strong>clause looks like this:<\/p>\n<p>&nbsp;<\/p>\n<p><em>access <\/em>class <em>classname <\/em>[extends <em>superclass<\/em>]<\/p>\n<p>[implements <em>interface <\/em>[,<em>interface&#8230;<\/em>]] {<\/p>\n<p>\/\/ class-body<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Here, <em>access <\/em>is either <strong>public <\/strong>or not used. If a class implements more than one interface, the interfaces are separated with a comma. If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface. The methods that implement an interface must be declared <strong>public<\/strong>. Also, the type signature of the implementing method must match exactly the type signature specified in the <strong>interface <\/strong>definition. Here is a small example class that implements the <strong>Callback <\/strong>interface shown earlier.<\/p>\n<p>&nbsp;<\/p>\n<p>class Client implements Callback {<\/p>\n<p>\/\/ Implement Callback&#8217;s interface<\/p>\n<p>public void callback(int p) {<\/p>\n<p>System.out.println(&#8220;callback called with &#8221; + p);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Notice that <strong>callback( ) <\/strong>is declared using the <strong>public <\/strong>access specifier. <em>When you implement an interface method, it must be declared as <strong>public<\/strong>. <\/em>It is both permissible and common for classes that implement interfaces to define additional members of their own. For example, the following version of <strong>Client <\/strong>implements <strong>callback( ) <\/strong>and adds the method <strong>nonIfaceMeth( )<\/strong>:<\/p>\n<p>&nbsp;<\/p>\n<p>class Client implements Callback {<\/p>\n<p>\/\/ Implement Callback&#8217;s interface<\/p>\n<p>public void callback(int p) {<\/p>\n<p>System.out.println(&#8220;callback called with &#8221; + p);<\/p>\n<p>}<\/p>\n<p>void nonIfaceMeth() {<\/p>\n<p>System.out.println(&#8220;Classes that implement interfaces &#8221; +<\/p>\n<p>&#8220;may also define other members, too.&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<h3><a name=\"_Toc179309434\"><\/a>Accessing Implementations through Interface References<\/h3>\n<p>&nbsp;<\/p>\n<p>You can declare variables as object references that use an interface rather than a class type. Any instance of any class that implements the declared interface can be referred to by such a variable. When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces. The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them. The calling code can dispatch through an interface without having to know anything about the \u201ccallee.\u201d This process is similar to using a superclass reference to access a subclass object, as described earlier. The following example calls the <strong>callback( ) <\/strong>method via an interface reference variable:<\/p>\n<p>&nbsp;<\/p>\n<p>class TestIface {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>Callback c = new Client();<\/p>\n<p>c.callback(42);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output of this program is shown here:<\/p>\n<p>callback called with 42<\/p>\n<p>&nbsp;<\/p>\n<p>Notice that variable <strong>c <\/strong>is declared to be of the interface type <strong>Callback<\/strong>, yet it was assigned an instance of <strong>Client<\/strong>. Although <strong>c <\/strong>can be used to access the <strong>callback( ) <\/strong>method, it cannot access any other members of the <strong>Client <\/strong>class. An interface reference variable only has knowledge of the methods declared by its <strong>interface <\/strong>declaration. Thus, <strong>c <\/strong>could not be used to access <strong>nonIfaceMeth( ) <\/strong>since it is defined by <strong>Client <\/strong>but not <strong>Callback<\/strong>. While the preceding example shows, mechanically, how an interface reference variable can access an implementation object, it does not demonstrate the polymorphic power of such a reference. To sample this usage, first create the second implementation of <strong>Callback<\/strong>, shown here:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Another implementation of Callback.<\/p>\n<p>class AnotherClient implements Callback {<\/p>\n<p>\/\/ Implement Callback&#8217;s interface<\/p>\n<p>public void callback(int p) {<\/p>\n<p>System.out.println(&#8220;Another version of callback&#8221;);<\/p>\n<p>System.out.println(&#8220;p squared is &#8221; + (p*p));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Now, try the following class:<\/p>\n<p>&nbsp;<\/p>\n<p>class TestIface2 {<\/p>\n<p>public static void main(String args[]) {<\/p>\n<p>Callback c = new Client();<\/p>\n<p>AnotherClient ob = new AnotherClient();<\/p>\n<p>c.callback(42);<\/p>\n<p>c = ob; \/\/ c now refers to AnotherClient object<\/p>\n<p>c.callback(42);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The output from this program is shown here:<\/p>\n<p>callback called with 42<\/p>\n<p>Another version of callback<\/p>\n<p>p squared is 1764<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, the version of <strong>callback( ) <\/strong>that is called is determined by the type of object that <strong>c <\/strong>refers to at run time. While this is a very simple example, you will see another, more practical one shortly.<\/p>\n<h3><a name=\"_Toc179309435\"><\/a>Partial Implementations<\/h3>\n<p>If a class includes an interface but does not fully implement the methods defined by that interface, then that class must be declared as <strong>abstract<\/strong>. For example:<\/p>\n<p>&nbsp;<\/p>\n<p>abstract class Incomplete implements Callback {<\/p>\n<p>int a, b;<\/p>\n<p>void show() {<\/p>\n<p>System.out.println(a + &#8221; &#8221; + b);<\/p>\n<p>}<\/p>\n<p>\/\/ &#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Here, the class <strong>Incomplete <\/strong>does not implement <strong>callback( ) <\/strong>and must be declared as abstract. Any class that inherits <strong>Incomplete <\/strong>must implement <strong>callback( ) <\/strong>or be declared <strong>abstract <\/strong>itself.<\/p>\n<h2><a name=\"_Toc179309436\"><\/a>Interfaces Can Be Extended<\/h2>\n<p>One interface can inherit another by use of the keyword <strong>extends<\/strong>. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain. Following is an example:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ One interface can extend another.<\/p>\n<p>interface A {<\/p>\n<p>void meth1();<\/p>\n<p>void meth2();<\/p>\n<p>}<\/p>\n<p>\/\/ B now includes meth1() and meth2() &#8212; it adds meth3().<\/p>\n<p>interface B extends A {<\/p>\n<p>void meth3();<\/p>\n<p>}<\/p>\n<p>\/\/ This class must implement all of A and B<\/p>\n<p>class MyClass implements B {<\/p>\n<p>public void meth1() {<\/p>\n<p>System.out.println(&#8220;Implement meth1().&#8221;);<\/p>\n<p>}<\/p>\n<p>public void meth2() {<\/p>\n<p>System.out.println(&#8220;Implement meth2().&#8221;);<\/p>\n<p>}<\/p>\n<p>public void meth3() {<\/p>\n<p>System.out.println(&#8220;Implement meth3().&#8221;);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>class IFExtend {<\/p>\n<p>public static void main(String arg[]) {<\/p>\n<p>MyClass ob = new MyClass();<\/p>\n<p>ob.meth1();<\/p>\n<p>ob.meth2();<\/p>\n<p>ob.meth3();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Any class that implements an interface must implement all methods defined by that interface, including any that are inherited from other interfaces.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interfaces &nbsp; Using the keyword interface, you can fully abstract a class\u2019 interface from its implementation. That is, using interface, you can specify what a class must do, but not&hellip; <\/p>\n","protected":false},"author":1,"featured_media":3472,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3447"}],"collection":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/comments?post=3447"}],"version-history":[{"count":2,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3447\/revisions"}],"predecessor-version":[{"id":3486,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/posts\/3447\/revisions\/3486"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media\/3472"}],"wp:attachment":[{"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/media?parent=3447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/categories?post=3447"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/softlect.com\/index.php\/wp-json\/wp\/v2\/tags?post=3447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}