{"id":79147,"date":"2020-06-20T09:00:43","date_gmt":"2020-06-20T03:30:43","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79147"},"modified":"2020-06-20T09:00:43","modified_gmt":"2020-06-20T03:30:43","slug":"java-extends-keyword","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/","title":{"rendered":"Java extends Keyword with Examples"},"content":{"rendered":"<p>In this article, we will discuss one of the most important and widely used <strong>keywords in Java<\/strong> which is the <strong>extends<\/strong> keyword. It is a reserved word that and we can&#8217;t use it as an identifier other than the places where we need to inherit the classes in Java. We use the extends keyword in <strong>Inheritance in Java<\/strong>.<\/p>\n<p>Inheritance is one of the object-oriented concepts which defines the ability of a class to extend or acquire the properties of another class. The extends keyword plays a significant role in implementing the Inheritance concept in Java. Let\u2019s start discussing the extends keyword with examples.<\/p>\n<h3>Java extends Keyword<\/h3>\n<p>The extends keyword in Java indicates that the child class inherits or acquires all the properties of the parent class. This keyword basically establishes a relationship of an inheritance among classes.<\/p>\n<p>If a class extends another class, then we say that it has acquired all the properties and behavior of the parent class.<\/p>\n<p>We use the extends keyword in Java between two class names that we want to connect in the Inheritance relationship.<\/p>\n<p>The class that extends the properties is the child class or the derived class which comes before the extends keyword, while the class from which the properties are inherited is the parent class or superclass or the base class, and this class comes after the extends keyword.<\/p>\n<p>It is not possible to extend multiple classes in Java because there is no support for multiple inheritances in Java. And therefore we cannot write multiple class names after the extended keyword.<\/p>\n<p>But, multiple classes can inherit from a single class as java supports hierarchical inheritance. Therefore we can write multiple class names before the extended keyword.<\/p>\n<h4>Syntax of extends keyword in Java<\/h4>\n<p>Following is the syntax of using extends keyword in java when using inheritance:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Parent {\n  \/\/code inside the parent class\n}\nclass Child extends Parent {\n  \/\/Code inside the child class\n}<\/pre>\n<p>The two important categories are:<\/p>\n<p><strong>a. Parent class-<\/strong> This is the class being inherited. Also called superclass or base class.<\/p>\n<p><strong>b. Child class-<\/strong> This class inherits the properties from the parent class. Also called a subclass or derived class.<\/p>\n<h4>Example of Java extends keyword<\/h4>\n<p>Let\u2019s see an example that uses the extends keyword in Java. The child class acquires all the accessible properties from its parent class and can also add some additional properties inside it to provide more information.<\/p>\n<p>Therefore when we access this class, we get all the properties from the parent class as well as the child class.<\/p>\n<p>For example, we have a Calculator class that has methods named add() and subtract(). Now we create a class called MyCalculator that extends the Calculator class. We want to add the function of multiplication and modulus in it, then we can add two methods named multiply() and modulus().<\/p>\n<p>Now when we access this class, then we can utilize all the four functionalities named-add(), subtract(), multiply() and modulus(). Let\u2019s see the implementation of this example:<\/p>\n<p><strong>Code to understand Java extends:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.extendskeyword;\nclass Calculator {\n  public int result;\n\n  public void add(int num1, int num2) {\n    result = num1 + num2;\n    System.out.println(\"The sum of the given numbers: \" + result);\n  }\n  public void subtract(int num1, int num2) {\n    result = num1 - num2;\n    System.out.println(\"The difference between the given numbers: \" + result);\n  }\n  public void multiply(int num1, int num2) {\n    result = num1 * num2;\n    System.out.println(\"The product of the given numbers: \" + result);\n  }\n}\nclass MyCalculator extends Calculator {\n  public void divide(int num1, int num2) {\n    result = num1 \/ num2;\n\n    System.out.println(\"The division of the given numbers: \" + result);\n  }\n  public void modulus(int num1, int num2) {\n    result = num1 % num2;\n    System.out.println(\"The remainder of the given numbers: \" + result);\n  }\n}\npublic class ExtendsDemo {\n  public static void main(String args[]) {\n    int number1 = 25,\n    number2 = 10;\n    MyCalculator object = new MyCalculator();\n    object.add(number1, number2);\n    object.subtract(number1, number2);\n    object.multiply(number1, number2);\n    object.divide(number1, number2);\n    object.modulus(number1, number2);\n  }\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">The sum of the given numbers: 35<br \/>\nThe difference between the given numbers: 15<br \/>\nThe product of the given numbers: 250<br \/>\nThe division of the given numbers: 2<br \/>\nThe remainder of the given numbers: 5<\/div>\n<h3>Extending Final Class in Java<\/h3>\n<p>If a class declares as final, then we can\u2019t extend or inherit it using the extends keyword. If we try to extend the final classes, then we will get a compilation error. We can make the class as final when we do not want any class to inherit its properties. For example:<\/p>\n<p><strong>Code to understand that we cannot extend the final class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.extendskeyword;\nfinal class Parent {\n  public int number1 = 5;\n  public int number2 = 10;\n\n  public void display() {\n    System.out.println(\"The first number is: \" + number1);\n    System.out.println(\"The second number is: \" + number2);\n\n  }\n}\nclass Child extends Parent {\n  int result;\n  public void sum() {\n    result = number1 + number2;\n    System.out.println(\"Sum of two numbers is: \" + result);\n  }\n}\npublic class Demo {\n  public static void main(String args[]) {\n    Child c = new Child();\n    c.display();\n    c.sum();\n  }\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Demo.java:12: error: cannot inherit from final Parent<br \/>\nclass Child extends Parent<br \/>\n^<strong>If we remove the final keyword, then we will get the output:<\/strong><br \/>\nThe first number is: 5<br \/>\nThe second number is: 10<br \/>\nSum of two numbers is: 15<\/div>\n<h3>Extending Interfaces in Java<\/h3>\n<p>The extends keyword can also be used to inherit the interfaces in java similar to that of classes. As one class can extend another class, similarly an interface can also extend interface in java. The child interface inherits the methods of its parent interface.<\/p>\n<p><strong>Code snippet to demonstrate the extending of interfaces using extends keyword:<\/strong><\/p>\n<p><strong>\/\/ Filename: Item.java<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public interface Item {\n  public void setItemId(int id);\n  public void setItemName(String name);\n}<\/pre>\n<p><strong>\/\/ Filename: Laptop.java<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public interface Laptop extends Item {\n  public void modelNumber(int number);\n  public void brandName(String brandName);\n  public void price(double price);\n}<\/pre>\n<h3>Conclusion<\/h3>\n<p>So, here we come to the end of this article of extends keyword in Java. You might have understood the significance and usage of the extends keyword in Java. We can extend both classes and interfaces in Java using the extends keyword.<\/p>\n<p>We have learned the keyword with real-life examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will discuss one of the most important and widely used keywords in Java which is the extends keyword. It is a reserved word that and we can&#8217;t use it as&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79186,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2904,2905,2325,2906,2907],"class_list":["post-79147","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-extends-keyword-in-java","tag-java-extend-multiple-classes","tag-java-extends","tag-java-extends-example","tag-java-extends-keyword"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java extends Keyword with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn java extends - What is it, syntax and examples of extends in java, Extending final class in java example, Extending Interfaces in java.\" \/>\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-keyword\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java extends Keyword with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn java extends - What is it, syntax and examples of extends in java, Extending final class in java example, Extending Interfaces in java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/\" \/>\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-06-20T03:30:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Extend-Keyword-in-Java-1.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":"Java extends Keyword with Examples - TechVidvan","description":"Learn java extends - What is it, syntax and examples of extends in java, Extending final class in java example, Extending Interfaces in java.","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-keyword\/","og_locale":"en_US","og_type":"article","og_title":"Java extends Keyword with Examples - TechVidvan","og_description":"Learn java extends - What is it, syntax and examples of extends in java, Extending final class in java example, Extending Interfaces in java.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-06-20T03:30:43+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Extend-Keyword-in-Java-1.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-keyword\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java extends Keyword with Examples","datePublished":"2020-06-20T03:30:43+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/"},"wordCount":744,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Extend-Keyword-in-Java-1.jpg","keywords":["extends keyword in java","java extend multiple classes","Java Extends","Java Extends example","java extends keyword"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/","url":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/","name":"Java extends Keyword with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Extend-Keyword-in-Java-1.jpg","datePublished":"2020-06-20T03:30:43+00:00","description":"Learn java extends - What is it, syntax and examples of extends in java, Extending final class in java example, Extending Interfaces in java.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Extend-Keyword-in-Java-1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Extend-Keyword-in-Java-1.jpg","width":802,"height":420,"caption":"Java extends keyword"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-extends-keyword\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java extends Keyword 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\/79147","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=79147"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79147\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79186"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}