In: Computer Science
Based on the data definition classes identified below, assume you wish to create a Dictionary class that inherits from the Book class. In addition to title and cost, the Dictionary class must also store the number of words present in the dictionary. Write the code that will create a constructor in the dictionary class that passes in and sets the dictionary's title, cost, and number of words.
public class Book {
private String title;
private double cost;
public Book(String title) {
this(title, 0.00);
}
public Book(String title, double cost) {
this.title = title;
this.cost = cost;
}
public String getTitle() { return this.title;
}
public double getCost() { return this.cost;
}
}
public class Dictionary extends Book {
private double numWords;
// Create Contructor
}
Code is Given Below:
============================
public class Dictionary extends Book {
private double numWords;
// Create constructor
public Dictionary(String title, double cost, double
numWords) {
//calling super class
constructor.
super(title, cost);
this.numWords = numWords;
}
}
Code Snapshot:
================
Explantion:-
===================
Here Dictionary class is child class of Book class. In Dictionary class we calling Book class constructor public Book(String title, double cost). By using super(title, cost); and setting numWords by using this.numWords = numWords;