{"id":2107,"date":"2018-05-24T09:46:00","date_gmt":"2018-05-24T09:46:00","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2107"},"modified":"2024-08-09T09:07:11","modified_gmt":"2024-08-09T09:07:11","slug":"write-a-program-to-show-inheritance-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-program-to-show-inheritance-in-java\/","title":{"rendered":"Write a program to show Inheritance in Java"},"content":{"rendered":"<h2>Write a program to show Inheritance in Java<\/h2>\r\n<p>In this post, we will define an abstract super class Book.java and define two sub classes Novel and TextBook. In Java we can use the <strong>extends <\/strong>keyword\u00a0 to subclass from the parent class using Inheritance.<\/p>\r\n<h3>Abstract Book class<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/**************************************************\r\n * Book.java\r\n * @program   \t: Book is an abstract class\r\n * @web        \t: www.TestingDocs.com\r\n * @version     : \r\n **************************************************\/\r\npublic abstract class Book {\r\n  \r\n  private String title;\r\n  private String ISBN;\r\n\r\n  public Book(String title, String iSBN) {\r\n    this.title = title;\r\n    this.ISBN = iSBN;\r\n  }\r\n  \r\n  public String getTitle() {\r\n    return title;\r\n  }\r\n\r\n  public void setTitle(String title) {\r\n    this.title = title;\r\n  }\r\n\r\n  public String getISBN() {\r\n    return ISBN;\r\n  }\r\n\r\n  public void setISBN(String iSBN) {\r\n    ISBN = iSBN;\r\n  }\r\n\r\n  @Override\r\n  public String toString() {\r\n    return \" title = \" + title + \" , ISBN=\" + ISBN ;\r\n  }\r\n}\r\n<\/pre>\r\n<h3>Novel Subclass<\/h3>\r\n<p>Novel is the subclass of Book.<\/p>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/**************************************************\r\n * Novel.java\r\n * @program   \t: Novel is a subclass of Book\r\n * @web        \t: www.TestingDocs.com\r\n * @author      :  \r\n * @version     : \r\n **************************************************\/\r\npublic class Novel extends Book {\r\n  private String genre;\r\n\r\n  public Novel(String title, String iSBN,String genre) {\r\n    super(title, iSBN);\r\n    this.genre=genre;\r\n  }\r\n\r\n  public String getGenre() {\r\n    return genre;\r\n  }\r\n\r\n  public void setGenre(String genre) {\r\n    this.genre = genre;\r\n  }\r\n\r\n  @Override\r\n  public String toString() {\r\n    return \"Novel = { genre=\" + genre + \" , \" + super.toString() + \"}\";\r\n  }\r\n}\r\n<\/pre>\r\n<h3>TextBook subclass<\/h3>\r\n<p>TextBook is a subclass of Book.<\/p>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/**************************************************\r\n * TextBook.java\r\n * @program   \t: TextBook is a subclass of Book\r\n * @web        \t: www.TestingDocs.com\r\n * @author      :  \r\n * @version     : \r\n **************************************************\/\r\npublic class TextBook extends Book {\r\n\r\n  private String course;\r\n  \r\n  public TextBook(String title, String iSBN) {\r\n    super(title, iSBN);\r\n\r\n  }\r\n\r\n  public TextBook(String title, String iSBN,String course) {\r\n    super(title, iSBN);\r\n    this.course=course;\r\n  }\r\n\r\n  public String getCourse() {\r\n    return course;\r\n  }\r\n\r\n\r\n  public void setCourse(String course) {\r\n    this.course = course;\r\n  }\r\n\r\n  @Override\r\n  public String toString() {\r\n    return \"TextBook = { course=\" + course + \" , \" +  super.toString() + \"}\";\r\n  }\r\n}\r\n<\/pre>\r\n<h3>TestDriver<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/**************************************************\r\n * TestDriver.java\r\n * @program   \t: TestDriver is the driver program\r\n * @web        \t: www.TestingDocs.com\r\n * @author      :  \r\n * @version     : \r\n **************************************************\/ \r\npublic class TestDriver {\r\n\r\n  public static void main(String[] args) {\r\n    System.out.println(\"------------Sample Output----------\");\r\n    TextBook textBook = new TextBook(\"Software Testing\",\"ISBN7039\",\"ST201\");\r\n    System.out.println(textBook.toString());\r\n    System.out.println();\r\n    \r\n    Novel novelBook = new Novel(\"Cold Comfort Farm\",\"ISBN7040\",\"Comedy\");\r\n    System.out.println(novelBook.toString());\r\n    System.out.println();\r\n  }\r\n}\r\n<\/pre>\r\n<h3>Program Output<\/h3>\r\n<p>&#8212;&#8212;&#8212;&#8212;Sample Output&#8212;&#8212;&#8212;-<br \/>TextBook = { course=ST201 , title = Software Testing , ISBN=ISBN7039}<\/p>\r\n<p>Novel = { genre=Comedy , title = Cold Comfort Farm , ISBN=ISBN7040}<\/p>\r\n<h3>Screenshot<\/h3>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2109\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inheritance-Java-Program-.png\" alt=\"\" width=\"1730\" height=\"987\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inheritance-Java-Program-.png 1730w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inheritance-Java-Program--300x171.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inheritance-Java-Program--1024x584.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inheritance-Java-Program--768x438.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inheritance-Java-Program--1536x876.png 1536w\" sizes=\"auto, (max-width: 1730px) 100vw, 1730px\" \/><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Write a program to show Inheritance in Java In this post, we will define an abstract super class Book.java and define two sub classes Novel and TextBook. In Java we can use the extends keyword\u00a0 to subclass from the parent class using Inheritance. Abstract Book class \/************************************************** * Book.java * @program : Book is an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2107","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2107"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2107\/revisions"}],"predecessor-version":[{"id":23852,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2107\/revisions\/23852"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}