{"id":77649,"date":"2020-04-08T12:55:26","date_gmt":"2020-04-08T07:25:26","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=77649"},"modified":"2020-04-08T12:55:26","modified_gmt":"2020-04-08T07:25:26","slug":"java-extends-vs-implements","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/","title":{"rendered":"Difference between Extends and Implements in Java with Examples"},"content":{"rendered":"<p>After the article of Java Autoboxing and Unboxing in Java, we will learn the <strong>difference between extends<\/strong> <strong>and implements in java<\/strong>. Both of them are the reserved keywords in Java which we use to inherit the features of an already existing parent block in the newly created child block.<\/p>\n<p>They are used to implement different concepts in Java. We will learn what is Java extends keyword and what is Java implements keyword with examples.<\/p>\n<h3>What is extends in Java?<\/h3>\n<ul>\n<li>The extends keyword in Java is useful when we want to inherit the properties and methods of a parent class in our child class.<\/li>\n<li>This extends keyword establishes the inheritance relationship between two classes.<\/li>\n<li>We use it while creating a derived class from the parent class or creating a subclass form the superclass.<\/li>\n<li>The rule is that a class can extend a maximum of one class only.<\/li>\n<li>When a class extends another class, it can override the methods defined in a superclass.<\/li>\n<li>We also use the extends keyword when we want an interface to extend or inherit another interface.<\/li>\n<\/ul>\n<p><strong>Code to understand the extends keyword in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.extendsvsimplements;\n\/\/Parent class\nclass Country\n{\n    String name = \"India\";\n    public void display()\n    {\n        System.out.println(\"This is my country India\");\n    }\n}\n\/\/child class extends the parent class\nclass City extends Country\n{   \n    \/\/ defining additional properties to the derived class\n    String state = \u201cRajasthan\u201d;\n    String city = \u201cJaipur\u201d;\n}\npublic class ExtendsDemo\n{\n    public static void main(String args[])\n    {\n        City obj=new City();\n        obj.display();\n        System.out.println(\"Name of the city is: \" + obj.name);\n        System.out.println(\"City is present in \" + obj.state + \u201cstate\u201d);\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is my country India<br \/>\nName of the city is: Jaipur<br \/>\nCity is present in Rajasthan state<\/div>\n<h4>What is Implements in Java?<\/h4>\n<ul>\n<li>We use the keyword implements while implementing an interface.<\/li>\n<li>A class can implement an interface using the implements keyword.<\/li>\n<li>An interface is an <em><strong>abstract type in Java<\/strong><\/em> that contains a collection of abstract methods. We cannot instantiate an interface that is, we can\u2019t create objects from an interface.<\/li>\n<li>When a class implements an interface, it has to define all the methods that are present inside the interface by providing the implementation of each of the methods.<\/li>\n<li>If the class that implements an interface, doesn\u2019t define or does not contains implementation of all the methods of the interface, then we must declare it as abstract. Otherwise, the compiler will give an error.<\/li>\n<li>Java doesn\u2019t support multiple inheritances, but we can implement multiple inheritance in our code by using multiple interfaces in Java.<\/li>\n<li>An interface can never implement another interface. The reason is that if the interface will implement another interface then it has to define all the methods of the interface which it is implementing but the interface can not have any concrete methods, therefore, an interface can\u2019t implement another interface in Java.<\/li>\n<\/ul>\n<p><strong>Code to understand implementation of java implements keyword:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.extendsvsimplements;\n\/\/Creating an interface\ninterface MyInterface\n{\n  \/\/Declaring abstract methods inside it\n  void displayString(String s);\n  void showNumber(int i);\n}\n\/\/Implementing the interface using the implements keyword\npublic class MyClass implements MyInterface\n{\n  \/\/Implementing all the methods declared in the interface\n  public void displayString(String s)\n  {\n    System.out.println(\"The string value is: \" +s);\n  }\n  public void showNumber(int i)\n  {\n    System.out.println(\"The integer value is: \" +i);\n  }\n  public static void main(String args[]) \n  {\n    MyClass obj = new MyClass();\n    obj.displayString(\"TechVidvan\");\n    obj.showNumber(20);\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The string value is: TechVidvan<br \/>\nThe integer value is: 20<\/div>\n<h4>Example of Multiple Interfaces<\/h4>\n<p>Now we will see an example in which one class implements more than one interface at the same time and therefore establishes multiple inheritances in Java. We will create 3 interfaces: Walkable, Swimmable, and Talkable and one class HumanBeing that implements all the 3 interfaces.<\/p>\n<h5>1. interface: Walkable.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.extendsvsimplements;\npublic interface Walkable \n{\n  public void walk();\n}<\/pre>\n<h5>2. interface: Swimmable.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.extendsvsimplements;\npublic interface Swimmable\n{\n  public void swim();\n}<\/pre>\n<h5>3. interface: Talkable.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.extendsvsimplements;\npublic interface Talkable \n{\n  public void talk();\n}<\/pre>\n<h5>4. Class: HumanBeing.java<\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.extendsvsimplements;\n\\\\To implement multiple interfaces we must use commas to separate them\npublic class HumanBeing  implements Walkable, Swimmable, Talkable\n{\n  @Override\n  public void walk()\n  {\n    System.out.println(\"I can walk\");\n  }\n  @Override\n  public void swim()\n  {\n    System.out.println(\"I can swim\");\n  }\n  @Override\n  public void talk()\n  {\n    System.out.println(\"I can talk\");\n  }\n  public static void main(String args[])\n  {\n    HumanBeing human= new HumanBeing();\n    human.walk();\n    human.swim();\n    human.talk();\n  }\n}<\/pre>\n<h4><strong>Output:<\/strong><\/h4>\n<div class=\"code-output\">I can walk<br \/>\nI can swim<br \/>\nI can talk<\/div>\n<h4>Difference between Extends and\u00a0 Implements in Java<\/h4>\n<p>We have seen the examples of both keywords, let us now see java extends vs implements in detail:<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Comparison Features<\/b><\/td>\n<td><b>Extends<\/b><\/td>\n<td><b>Implements<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>Implementation<\/b><\/td>\n<td>The keyword <b>extends<\/b> is used when a class wants to inherit all the properties from another class or an interface that wants to inherit an interface.<\/td>\n<td>We use the <b>implements <\/b>keyword when we want a class to implement an interface.<\/td>\n<\/tr>\n<tr>\n<td><b>Associated with<\/b><\/td>\n<td>It is associated with Inheritance<\/td>\n<td>It is associated with Abstraction<\/td>\n<\/tr>\n<tr>\n<td><b>Method<\/b><\/td>\n<td>The child class that extends a parent class may or may not override all the methods present in the parent class.<\/td>\n<td>The class that implements an interface must define or provide the implementation of all the methods declared in the interface, or else the class should be declared as abstract.<\/td>\n<\/tr>\n<tr>\n<td><b>Class<\/b><\/td>\n<td>A subclass or more than one subclass can extend only one parent class at the same time.<\/td>\n<td>A class can implement one or more than one interface at the same time.<\/td>\n<\/tr>\n<tr>\n<td><b>Interface<\/b><\/td>\n<td>An interface can extend any number of interfaces.<\/td>\n<td>An interface can never implement any other interface.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Summary<\/h3>\n<p>So in conclusion, we come to the end of the article of difference between extends and implements in java. We went through the major differences between these keywords. Both of them directly or indirectly use the concept of inheritance in Java.<\/p>\n<p>But, we can conclude that implementing an interface provides more flexibility than extending a class as we can extend a single class but we can implement multiple inheritances in Java.<\/p>\n<p>Hope, after reading this article, you might have finally understood extends vs implements keyword in Java.<\/p>\n<p>Thank you for reading our article. Do share your feedback through the comment section below.<\/p>\n<p>Happy Learning \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After the article of Java Autoboxing and Unboxing in Java, we will learn the difference between extends and implements in java. Both of them are the reserved keywords in Java which we use to&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78144,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2323,2324,2325,2326,2327,2328],"class_list":["post-77649","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-difference-between-extends-and-implements-in-java","tag-extends-vs-implements","tag-java-extends","tag-java-extends-and-implements","tag-java-extends-vs-java-implements","tag-java-implements"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Difference between Extends and Implements in Java with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn the difference between extends and implements in java which are 2 java keywords. Learn java extends and java implements with examples and differences.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Difference between Extends and Implements in Java with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn the difference between extends and implements in java which are 2 java keywords. Learn java extends and java implements with examples and differences.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-08T07:25:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/difference-between-java-extends-vs-implements.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Difference between Extends and Implements in Java with Examples - TechVidvan","description":"Learn the difference between extends and implements in java which are 2 java keywords. Learn java extends and java implements with examples and differences.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/","og_locale":"en_US","og_type":"article","og_title":"Difference between Extends and Implements in Java with Examples - TechVidvan","og_description":"Learn the difference between extends and implements in java which are 2 java keywords. Learn java extends and java implements with examples and differences.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-08T07:25:26+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/difference-between-java-extends-vs-implements.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Difference between Extends and Implements in Java with Examples","datePublished":"2020-04-08T07:25:26+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/"},"wordCount":778,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/difference-between-java-extends-vs-implements.jpg","keywords":["Difference between Extends and Implements in Java","Extends vs Implements","Java Extends","Java Extends and Implements","java extends vs java implements","Java Implements"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/","url":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/","name":"Difference between Extends and Implements in Java with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/difference-between-java-extends-vs-implements.jpg","datePublished":"2020-04-08T07:25:26+00:00","description":"Learn the difference between extends and implements in java which are 2 java keywords. Learn java extends and java implements with examples and differences.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/difference-between-java-extends-vs-implements.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/difference-between-java-extends-vs-implements.jpg","width":802,"height":420,"caption":"Difference between extends and implements in java"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-vs-implements\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Difference between Extends and Implements in Java with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=77649"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77649\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78144"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}