{"id":506,"date":"2021-01-07T17:21:28","date_gmt":"2021-01-07T17:21:28","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=506"},"modified":"2021-01-07T17:49:44","modified_gmt":"2021-01-07T17:49:44","slug":"interfaces","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/","title":{"rendered":"Interfaces &#038; Multiple Inheritance"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a6d52e86634e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a6d52e86634e\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Whats_the_Problem\" >What&#8217;s the Problem?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#The_Solution\" >The Solution<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Why_is_that_better\" >Why is that better?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Multiple_Inheritance\" >Multiple Inheritance<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Single_vs_Multiple_Inheritance\" >Single vs. Multiple Inheritance<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#What_is_an_Interface\" >What is an Interface?<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Interface_%E2%80%9CContracts%E2%80%9D\" >Interface &#8220;Contracts&#8221;<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Nouns_vs_Adjectives\" >Nouns vs. Adjectives<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Interface_Inheritance\" >Interface Inheritance<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Superinterfaces_and_Subinterfaces\" >Superinterfaces and Subinterfaces<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/interfaces\/#Marker_Interfaces\" >Marker Interfaces<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">Suppose you wanted your <strong>Employee<\/strong> classes to have a standard behavior, such as the ability to be sorted &#8212; so that you could list your employees in a report, perhaps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You could add this capability to the&nbsp;<strong>Employee&nbsp;<\/strong>class, and all its subclasses would inherit that capability, also.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On further reflection, however, you realize that the capability of being sorted is potentially usable by other classes beyond just employees.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, you write an abstract class&nbsp;<strong>Sortable<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage sortable;\n\npublic abstract class Sortable\n{\n    public abstract int compare(Sortable rhs);\n\n    public static void shellSort(Sortable&#x5B;] a)\n    {\n        int n = a.length;\n        int incr = n\/2;\n        while (incr &gt;= 1)\n        {\n            for (int i = incr; i &lt; n; i++)\n            {\n                Sortable temp = a&#x5B;i];\n                int j = i;\n                while ((j &gt;= incr) &amp;&amp;\n                       (temp.compare(a&#x5B;j - incr]) &lt; 0)\n                      )\n                {\n                    a&#x5B;j] = a&#x5B;j - incr];\n                    j -= incr;\n                }\n                a&#x5B;j] =temp;\n            }\n            incr \/= 2;\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">And modify the&nbsp;<strong>Employee<\/strong>&nbsp;class to inherit from it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [6,9,10,11,12,13,14,15]; title: ; quick-code: false; notranslate\" title=\"\">\npackage company;\n\nimport java.util.Date;\nimport sortable.Sortable;\n\npublic class Employee extends Sortable\n{\n    \/\/ Compare, based on salary\n    public int compare(Sortable rhs)\n    {\n        Employee eb = (Employee) rhs;\n        if (m_salary &lt; eb.m_salary) return -1;\n        if (m_salary &gt; eb.m_salary) return 1;\n        return 0;\n    }\n    \/\/ ...\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Then, we can modify the test program to use the sorting capability:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [14,15,16]; title: ; quick-code: false; notranslate\" title=\"\">\npackage company;\n\nimport java.util.Date;\nimport sortable.Sortable;\n\npublic class CompanyTest\n{\n    public static void main(String&#x5B;] args)\n    {\n        Employee&#x5B;] staff = new Employee&#x5B;6];\n\n\t\/\/ ...\n\n        Sortable.shellSort(staff);\n        for (int i = 0; i &lt; staff.length; i++)\n            staff&#x5B;i].print();\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This works fine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Whats_the_Problem\"><\/span>What&#8217;s the Problem?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">So, you might say, what&#8217;s the problem?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What if you wanted to add additional capabilities to the employee classes, such as&nbsp;<strong>Persistent<\/strong>,&nbsp;<strong>Cloneable<\/strong>, etc.?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You could write an abstract class for each one, and then try to get class&nbsp;<strong>Employee<\/strong>&nbsp;to use them all:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [3,4,5,7]; title: ; quick-code: false; notranslate\" title=\"\">\npackage company;\n\nimport sortable.Sortable;\nimport persistent.Persistent;\nimport cloneable.Cloneable;  \/\/ Fictitious!\n\npublic class Employee extends Sortable, Persistent, Cloneable\n{\n    \/\/ ...\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Unfortunately, Java gives you compile-time errors if you try it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why?<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Solution\"><\/span>The Solution<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As you probably guessed, the solution is to use&nbsp;<em><strong>interfaces<\/strong><\/em>&nbsp;instead of classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an interface called <strong>Sortable<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [3]; title: ; quick-code: false; notranslate\" title=\"\">\npackage sortable;\n\npublic interface Sortable\n{\n    public int compare(Sortable rhs);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We have to place the&nbsp;<strong>shellSort()<\/strong>&nbsp;method in a separate class, because Java doesn&#8217;t let us implement any methods in an interface:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage sortable;\n\npublic abstract class Sort\n{\n    public static void shellSort(Sortable&#x5B;] a)\n    {\n        int n = a.length;\n        int incr = n\/2;\n        while (incr &gt;= 1)\n        {\n            for (int i = incr; i &lt; n; i++)\n            {\n                Sortable temp = a&#x5B;i];\n                int j = i;\n                while ((j &gt;= incr) &amp;&amp;\n                       (temp.compare(a&#x5B;j - incr]) &lt; 0)\n                      )\n                {\n                    a&#x5B;j] = a&#x5B;j - incr];\n                    j -= incr;\n                }\n                a&#x5B;j] =temp;\n            }\n            incr \/= 2;\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note that we made class <strong>Sort<\/strong> abstract, because we never need an instance of it; it contains exactly one method, which is a static method.&nbsp; (Another example of an abstract class that only contains static methods is the <strong>Math<\/strong> class.)&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And we must then modify the&nbsp;<strong>Employee<\/strong>&nbsp;class as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [5]; title: ; quick-code: false; notranslate\" title=\"\">\npackage Employees;\nimport java.util.Date;\nimport Sortable.Sortable;\n\npublic class Employee implements Sortable\n{\n    public int compare(Sortable rhs)\n    {\n        Employee eb = (Employee) rhs;\n        if (m_salary &lt; eb.m_salary) return -1;\n        if (m_salary &gt; eb.m_salary) return 1;\n        return 0;\n    }\n    \/\/ ...\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note that we use the&nbsp;<code><strong>implements<\/strong><\/code>&nbsp;keyword to specify that a class uses an interface, as opposed to the&nbsp;<code><strong>extends<\/strong><\/code>&nbsp;keyword.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The test code looks similar to before:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [4,14,15,16]; title: ; quick-code: false; notranslate\" title=\"\">\npackage company;\n\nimport java.util.Date;\nimport sortable.Sort;\n\npublic class CompanyTest\n{\n    public static void main(String&#x5B;] args)\n    {\n        Employee&#x5B;] staff = new Employee&#x5B;6];\n\n\t\/\/ ...\n\n        Sort.shellSort(staff);\n        for (int i = 0; i &lt; staff.length; i++)\n            staff&#x5B;i].print();\n    }\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_is_that_better\"><\/span>Why is that better?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">So, why do we think that using interfaces is better, in this case?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using interfaces in Java is essential for adding certain functionality.&nbsp; For example, we can now add other capabilities simultaneously using additional interfaces:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [7]; title: ; quick-code: false; notranslate\" title=\"\">\npackage company;\n\nimport sortable.Sortable;\nimport persistent.Persistent;\nimport cloneable.Cloneable; \/\/ Fictitious!\n\npublic class Employee implements Sortable, Persistent, Cloneable\n{\n    \/\/ ...\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note the keyword&nbsp;<strong>implements&nbsp;<\/strong>in place of&nbsp;<strong>extends<\/strong>, and the fact that you can now specify a comma-separated list of <em>interface<\/em> names.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is valid syntax, and compiles without error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Multiple_Inheritance\"><\/span>Multiple Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Single_vs_Multiple_Inheritance\"><\/span>Single vs. Multiple Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Java allows a class to&nbsp;<strong><em>inherit<\/em><\/strong>&nbsp;from only a <em>single superclass<\/em>.&nbsp; This is called&nbsp;<strong><em>single inheritance.<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In contrast, C++ supports <em>both single and multiple class inheritance<\/em>.\u00a0 Experience has shown, however, that using multiple inheritance is often problematic;\u00a0 it is very complex, rife with pitfalls, and is one of the most difficult and non-intuitive features of that language. <em>Using uncontrolled multiple inheritance can be dangerous to your sanity!<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, Java does allow a class to&nbsp;<strong><em>implement<\/em><\/strong>&nbsp;many interfaces.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We say that Java supports:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong>Single implementation inheritance (extend from a superclass)<\/strong><\/em><\/li><li><strong><em>Multiple interface inheritance (implement zero or more interfaces)<\/em><\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">As a result, Java avoids the horrible problems of generalized multiple inheritance.\u00a0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_an_Interface\"><\/span>What is an Interface?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">So, What is an Interface?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An&nbsp;<em>interface<\/em>&nbsp;has the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It is a&nbsp;<em><strong>type<\/strong><\/em>, just as a class is a type.<\/li><li>It may contain:<ul><li>abstract method declarations (no implementations).&nbsp;&nbsp;<ul><li>Such methods are always (implicitly)&nbsp;<strong>public<\/strong>.<\/li><\/ul><\/li><li>field (data) declarations that are implicitly&nbsp;<strong>public<\/strong>,&nbsp;<strong>static&nbsp;<\/strong>and&nbsp;<strong>final<\/strong>.&nbsp;<ul><li><strong>synchronized<\/strong>,&nbsp;<strong>transient&nbsp;<\/strong>and&nbsp;<strong>volatile<\/strong>&nbsp;are&nbsp;<em>not allowed<\/em><\/li><\/ul><\/li><\/ul><\/li><li>It may&nbsp;<em>not be instantiated<\/em><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Basically, an interface is similar to an abstract class that contains:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>no method implementations, and<\/li><li>no instance fields<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Interfaces allow you to &#8220;add in&#8221; multiple behaviors for a class. If you want to instantiate such a class, that class must implement all the abstract methods from all the interfaces it has said it implements.&nbsp; If that class does not implement the abstract methods from its interfaces, then that class is abstract.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ability for a class to implement a number of interfaces amounts to&nbsp;<em><strong>multiple interface inheritance<\/strong><\/em>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Java&#8217;s more restricted form of multiple inheritance is a much safer form than the more general multiple inheritance supported by C++. The trickiest area of multiple inheritance is when you are trying to inherit multiple implementations; if you restrict multiple inheritance to multiple interface inheritance, most of the problems go away.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Interface_%E2%80%9CContracts%E2%80%9D\"><\/span>Interface &#8220;Contracts&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">One way to look at an interface is to consider it to be a &#8220;<em>contract<\/em>&#8220;.\u00a0 Any class that specifies it implements that interface is &#8220;<em>contractually obligated<\/em>&#8221; to implement all the (abstract) methods specified in the interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Such a &#8220;<em>contract<\/em>&#8221; only specifies that there be an implementation, not how it might be implemented.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nouns_vs_Adjectives\"><\/span>Nouns vs. Adjectives<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One clue to help you choose whether to use a class or an interface has to do with&nbsp;<em>parts of speech<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you recall, in English, there are the following parts of speech:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Verbs<\/strong> (is, does, makes, travels, reads, invents, and so on)<\/li><li><strong>Nouns<\/strong> (book, class, student, thought, John, etc.)<\/li><li><strong>Pronouns<\/strong> (he, she, it, you, them, which, mine, etc.)<\/li><li><strong>Adjectives<\/strong> (wet, dry, dark, light, my, your, bedraggled, etc.)<\/li><li><strong>Adverbs<\/strong> (finely, well, quickly, unfortunately, etc.)<\/li><li><strong>Prepositions<\/strong> (on, about, among, behind, etc.)<\/li><li><strong>Conjunctions<\/strong> (and, but, for, after, because, etc.)<\/li><li><strong>Interjections<\/strong> (ouch!, yikes!, hey!, Gadzooks!, eh?\u00a0<em>[especially in Canada ;)]<\/em> )<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If you are designing your program in an Object-Oriented way, you should be thinking about the objects that your program will be dealing with.&nbsp; These objects can be relatively concrete, such as Person, Employee, Dog, Gorilla, etc., or they can be more ethereal, such as Idea, Relationship, Reputation, and so on.&nbsp; Each of these objects is therefore somewhat naturally represented by a name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A&nbsp;<strong><em>class<\/em><\/strong>&nbsp;represents such an object, and so the most natural name for such a class is a&nbsp;<strong><em>noun<\/em><\/strong>&nbsp;&#8212; the name of the object. Sometimes, it is easy to determine an appropriate name, and sometimes less so. (I am a firm believer in spending time choosing an appropriate name for a class, because it represents what my program is trying to model.&nbsp; If I choose wisely, many of the questions that arise can be answered relatively easily;&nbsp; if I do not, things get harder.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may have noticed that the\u00a0<strong><em>interfaces<\/em><\/strong>\u00a0we&#8217;ve looked at so far have names like <strong>Cloneable<\/strong>, <strong>Sortable<\/strong>, <strong>Serializable<\/strong>, and so on.\u00a0 These are\u00a0<strong><em>adjectives<\/em><\/strong>, rather than nouns, and this convention is fairly typical.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, here are some simple rules of thumb about naming:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Class &#8212; normally use a&nbsp;<em>noun<\/em><\/strong><\/li><li><strong>Interface &#8212; often use an&nbsp;<em>adjective<\/em>, but&nbsp;<em>nouns<\/em>&nbsp;are often used, too<\/strong><\/li><li><strong>Method &#8212; normally use an&nbsp;<em>imperative verb<\/em>.<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These aren&#8217;t iron-clad rules, but I would definitely think twice before violating them &#8212; and fully justify my choice!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Appropriate choices (which come more easily with experience and an awareness of need) will often make things easier for you as you progress in the design and implementation of your program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Interface_Inheritance\"><\/span>Interface Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Superinterfaces_and_Subinterfaces\"><\/span>Superinterfaces and Subinterfaces<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">As we have said, a class may implement zero or more <em>interfaces<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition, an interface may&nbsp;<em>inherit<\/em>&nbsp;(keyword&nbsp;<code>extends<\/code>) from&nbsp;<em><strong>one or more<\/strong><\/em>&nbsp;other&nbsp;<em><strong>superinterfaces<\/strong><\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>interface BaseColors\n{\n    int RED = 1, GREEN = 2, BLUE = 3;\n}\n<\/strong><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>interface RainbowColors extends BaseColors\n{\n    int YELLOW = 3, ORANGE = 5, INDIGO = 6, VIOLET = 7;\n}\n<\/strong><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>interface PrintColors extends BaseColors\n{\n    int YELLOW = 8, CYAN = 16, MAGENTA = 32;\n}\n<\/strong><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>interface FancyColors extends RainbowColors, PrintColors\n{\n    int FUCHSIA = 17,\n        VERMILLION = 43,\n        CHARTREUSE = RED + 90;\n}<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em><u>Note:<\/u><\/em><\/strong>&nbsp;Be careful with ambiguous inherited fields (<strong>YELLOW<\/strong>, above)!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The interfaces named in the&nbsp;<strong>extends&nbsp;<\/strong>clause for a class are the&nbsp;<em><strong>direct superinterfaces<\/strong><\/em>&nbsp;for that class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Any class that\u00a0<strong>implements\u00a0<\/strong>the declared interface is also considered to implement all the interfaces that this interface extends and that are accessible to the class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Marker_Interfaces\"><\/span>Marker Interfaces<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Surprisingly, it is possible to define an interface that is <em>entirely empty<\/em>!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why is this useful?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A class can implement this interface simply to provide additional information about itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We will soon see an example of a marker interface (<code><strong>Cloneable<\/strong><\/code>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you wanted your Employee classes to have a standard behavior, such as the ability to be sorted &#8212; so that you could list your employees in a report, perhaps. You could add this capability to the&nbsp;Employee&nbsp;class, and all its subclasses would inherit that capability, also. On further reflection, however, you realize that the capability [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":61,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_front_end_style_editor":"no","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-506","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Suppose you wanted your Employee classes to have a standard behavior, such as the ability to be sorted &#8212; so that you could list your employees in a report, perhaps. You could add this capability to the&nbsp;Employee&nbsp;class, and all its subclasses would inherit that capability, also. On further reflection, however, you realize that the capability&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=506"}],"version-history":[{"count":9,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/506\/revisions"}],"predecessor-version":[{"id":521,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/506\/revisions\/521"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/61"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}