Site icon TestingDocs.com

Write a program to show Inheritance in Java

Introduction

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  to subclass from the parent class using Inheritance.

Abstract Book class

/**************************************************
 * Book.java
 * @program   	: Book is an abstract class
 * @web        	: www.TestingDocs.com
 * @version     : 
 **************************************************/
public abstract class Book {
  
  private String title;
  private String ISBN;

  public Book(String title, String iSBN) {
    this.title = title;
    this.ISBN = iSBN;
  }
  
  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getISBN() {
    return ISBN;
  }

  public void setISBN(String iSBN) {
    ISBN = iSBN;
  }

  @Override
  public String toString() {
    return " title = " + title + " , ISBN=" + ISBN ;
  }
}

Novel Subclass

Novel is the subclass of Book.

/**************************************************
 * Novel.java
 * @program   	: Novel is a subclass of Book
 * @web        	: www.TestingDocs.com
 * @author      :  
 * @version     : 
 **************************************************/
public class Novel extends Book {
  private String genre;

  public Novel(String title, String iSBN,String genre) {
    super(title, iSBN);
    this.genre=genre;
  }

  public String getGenre() {
    return genre;
  }

  public void setGenre(String genre) {
    this.genre = genre;
  }

  @Override
  public String toString() {
    return "Novel = { genre=" + genre + " , " + super.toString() + "}";
  }
}

TextBook subclass

TextBook is a subclass of Book.

/**************************************************
 * TextBook.java
 * @program   	: TextBook is a subclass of Book
 * @web        	: www.TestingDocs.com
 * @author      :  
 * @version     : 
 **************************************************/
public class TextBook extends Book {

  private String course;
  
  public TextBook(String title, String iSBN) {
    super(title, iSBN);

  }

  public TextBook(String title, String iSBN,String course) {
    super(title, iSBN);
    this.course=course;
  }

  public String getCourse() {
    return course;
  }


  public void setCourse(String course) {
    this.course = course;
  }

  @Override
  public String toString() {
    return "TextBook = { course=" + course + " , " +  super.toString() + "}";
  }
}

TestDriver

/**************************************************
 * TestDriver.java
 * @program   	: TestDriver is the driver program
 * @web        	: www.TestingDocs.com
 * @author      :  
 * @version     : 
 **************************************************/ 
public class TestDriver {

  public static void main(String[] args) {
    System.out.println("------------Sample Output----------");
    TextBook textBook = new TextBook("Software Testing","ISBN7039","ST201");
    System.out.println(textBook.toString());
    System.out.println();
    
    Novel novelBook = new Novel("Cold Comfort Farm","ISBN7040","Comedy");
    System.out.println(novelBook.toString());
    System.out.println();
  }
}

Program Output

————Sample Output———-
TextBook = { course=ST201 , title = Software Testing , ISBN=ISBN7039}

Novel = { genre=Comedy , title = Cold Comfort Farm , ISBN=ISBN7040}

Screenshot

Exit mobile version