{"id":702,"date":"2010-08-06T18:58:33","date_gmt":"2010-08-06T13:28:33","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=702"},"modified":"2010-08-06T18:58:33","modified_gmt":"2010-08-06T13:28:33","slug":"difference-between-interfaceinheritance-abstract-class","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-interfaceinheritance-abstract-class\/","title":{"rendered":"Interface, Inheritance and abstract class"},"content":{"rendered":"<p><strong>Interface,Inheritance &amp; Abstract Class:-<\/strong><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/Inheritance1.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-736\" title=\"Inheritance, Interface and abstract class\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/Inheritance1-300x173.png?resize=300%2C173&#038;ssl=1\" alt=\"Inheritance, Interface and abstract class\" width=\"300\" height=\"173\" \/><\/a><\/p>\n<p><strong>Inheritance : <\/strong><\/p>\n<p><strong><\/strong>The concept of extending a class by another class is known as <strong>inheritance<\/strong>.<\/p>\n<p>A class which is inherited is known as Parent class, Base class or Super class.<\/p>\n<p>A class which extends base class is known as Derived class or child class.<\/p>\n<hr \/>\n<p><strong>Abstract Class: <\/strong><\/p>\n<p><strong><\/strong>If a class contains one or more than one abstract method (abstract method is defined by keyword abstract and contains only declaration of that method.) then the class is declared with keyword abstract and known as <strong>abstract class.<\/strong><\/p>\n<hr \/>\n<p><strong>Interface :<\/strong><\/p>\n<p>An Interface in java is known as Contract which must be followed by its implementing class (Child class).<\/p>\n<p>If a class contains all methods as abstract method then, the class can be declared as\u00a0<strong>Interface<\/strong>.<\/p>\n<p><strong>Important notes on interface:<\/strong><\/p>\n<ol>\n<li>only<strong> public and abstract<\/strong> modifiers are allowed to use in interface. (Static are not allowed)<\/li>\n<li>If variable is defined inside the interface, then it must be<strong> initialized and it is final<\/strong> by default.<\/li>\n<\/ol>\n<p><a title=\"Interface in JAVA\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/interface\/\" target=\"_blank\">To read more on interface, refer this article.<\/a><\/p>\n<hr \/>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\ninterface IMario\n{\n  Void behavior();\n}\n\nclass \u00a0Mario implements IMario\n{\n public void behavior()\n {\n     System.out.println(\"This is simple Mario\"\u009d);\n }\n}\n\nclass \u00a0SuperMario implements IMario\n{\n  public void behavior()\n  {\n     System.out.println(\"This is Super Mario with power\"\u009d);\n  }\n}\n\nclass \u00a0TestMario\n{\n   public static void main(String args&#x5B;])\n   {\n        System.out.println(\"----game started----\");\n        Imario obj=new Mario();\n        Obj.behavior();\n        System.out.println(-----After Mashroom-----\");\n        obj=new SuperMario();\n        Obj.behavior();\n        System.out.println(-----After Power Loss-----\");\n        obj=new Mario();\n        Obj.behavior();\n   }\n}\n<\/pre>\n<p><strong>o\/p:- <\/strong><\/p>\n<p><strong> <\/strong>&#8212;&#8211;Game Started&#8212;&#8212;<\/p>\n<p>This is <strong>simple Mario.<\/strong><\/p>\n<p>&#8212;&#8211;After Mashroom&#8212;&#8212;<\/p>\n<p>This is <strong>Super Mario with power.<\/strong><\/p>\n<p>&#8212;&#8211;After Power Loss&#8212;&#8211;<\/p>\n<p>This is <strong>simple Mario.<\/strong><\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\ninterface A\n{\n   void displayA()\n}\n\nabstract class B\n{\n   public void displayB()\n   {\n     System.out.println(\"Display-B\"\u009d);\n   }\n   abstract public void display();\n}\n\nclass \u00a0c extends B implements A\n{\n   Public void displayB()\n  {\n      System.out.println(\"Abstract Display-B\"\u009d);\n  }\n\n  public void displayA()\n {\n     System.out.println(\"Display-A\"\u009d);\n  }\n}\n\nclass \u00a0TestClass\n{\n  public static void main(String args&#x5B;])\n {\n   C obj=new C();\n   Obj.display();\n   Obj.displayA();\n   Obj.displayB();\n }\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is abstract class, interface and inheritance in java. Source code and example<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[32,122],"class_list":["post-702","post","type-post","status-publish","format-standard","hentry","category-java","tag-abstract","tag-interface"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":700,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/interface\/","url_meta":{"origin":702,"position":0},"title":"Interface in JAVA","author":"Jitendra","date":"August 2, 2010","format":false,"excerpt":"Explains the usage and benefits of interface in JAVA.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2017,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-j2ee-interview-questions-1\/","url_meta":{"origin":702,"position":1},"title":"Java &#8211; J2EE Interview Questions &#8211; 1","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"JAVA - J2EE Interview Questions - 1, JAVA - J2EE Interview Questions - 1,custom tag JSP, Externalization , serialVersionUID, difference between interface and abstract class, iterate HashMap","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1439,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-in-httpservlet-and-genericservlet\/","url_meta":{"origin":702,"position":2},"title":"Difference in HttpServlet and GenericServlet","author":"Jitendra","date":"February 4, 2011","format":false,"excerpt":"What is the basic Difference in HttpServlet and GenericServlet","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":757,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/anonymous-classimplements-interface-methods-of-threads\/","url_meta":{"origin":702,"position":3},"title":"Create Thread using Anonymous class and Interface","author":"Jitendra","date":"August 16, 2010","format":false,"excerpt":"Demonstration of creating Thread by Anonymous class and Interface","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1933,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/virtual-override-and-new-keyword-in-c\/","url_meta":{"origin":702,"position":4},"title":"Virtual, Override and new Keyword in C#","author":"Jitendra","date":"April 7, 2011","format":false,"excerpt":"Virtual, Override and new Keyword : Polymorphism , Method Hiding and Overriding in C#","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"Multilevel Inheritance","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Multilevel-Inheritance.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1676,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/meta-annotation-annotate-other-annotation\/","url_meta":{"origin":702,"position":5},"title":"Meta Annotation &#8211; annotate other annotation","author":"Jitendra","date":"March 16, 2011","format":false,"excerpt":"What is Meta Annotation and its usage in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Inbuilt Annotation and Meta Annotations in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Inbuilt-Annotation-and-Meta-Annotations-in-JAVA.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=702"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/702\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}