...
Private Variables: In addition to the private variables inherited from the
Publication
class (title
,author
,numberOfPages
), theTextBook
class introduces two additional private variables:version
(a string that represents the version of the textbook) andsubjectDomain
(a string that represents the textbook's subject area).Getters and Setters: These methods are used to get (
getVersion
andgetSubjectDomain
) and set (setVersion
andsetSubjectDomain
) the values of theversion
andsubjectDomain
variables.Constructor: The constructor for the
TextBook
class (which takes five parameters:title
,author
,numberOfPages
,version
, andsubjectDomain
) first calls thePublication
constructor (using thesuper
keyword) to initialize thetitle
,author
, andnumberOfPages
fields inherited fromPublication
. Then it sets theversion
andsubjectDomain
fields with the provided values.Overridden Method: The
PrintInformation
method is overridden inTextBook
. This method first calls the superclass'sPrintInformation
method to get a string of the general publication's information. Then it adds information about the textbook'sversion
andsubjectDomain
. This overriding of methods is a key aspect of polymorphism in object-oriented programming, allowing a subclass to provide a different implementation of a method that is already provided by its superclass.
In conclusion, the The TextBook
class is a more specific type of Publication
that includes extra information that is specifically relevant to textbooks, namely the version of the textbook and the subject area it covers. This represents an "is-a" relationship in object-oriented programming: a textbook is a publication.
Driver Program (Demo1Demo)
The ability to add different types of objects (i.e., TextBook
and Magazine
) to the same ArrayList<Publication>
and then iterate over them, treating each as a Publication
, demonstrates polymorphism. Each of these objects can have its own implementation of the PrintInformation
method (or other methods defined in the Publication
class), and the specific method that gets called is determined at runtime, depending on the actual type of the object in the list. This is an example of run-time polymorphism or dynamic method dispatch.
Code Block | ||
---|---|---|
| ||
/* Driver to demonstrate polymorphism using the Textbook and Magazine Class */ import java.time.LocalDate; import java.util.ArrayList; // import the ArrayList class import java.time.LocalDate; // for a date varaible public class Demo4Demo { public static void main(String[] args) { //create an Array List of Publications ArrayList<Publication> myPublications = new ArrayList<Publication>(); //now add some Magazines and textbook to the Publication ArrayList //adding textbooks myPublications.add(new TextBook("Learn Java", "Bart Simpson", 759, "8th Edition", "Computer Science") ); myPublications.add(new TextBook("Learn C++", "Fred Flintstone", 759, "7th Edition", "Computer Science") ); //create a date LocalDate myDate = LocalDate.of(2022, 8, 30); //adding magazines myPublications.add(new Magazine("Time Magazine", "Time Life Publisher", 83, "Monthly", myDate)); myPublications.add(new Magazine("Alamo College Magazine", "ACCC", 65, "Quarterly", myDate)); //now iterate through the array and display the magazines and textbooks for(Publication pPubs : myPublications) { System.out.println(pPubs.PrintInformation()); } }//end of Main }//end of class |
...