...
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)
...
language | java |
---|
...
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 Demo { public static void main(String[] args) { //create an instanceArray List of Magazine Publications MagazineArrayList<Publication> myMagazinemyPublications = new Magazine("Time Magazine", "Time Life Publisher", 83, "Monthly", myDate); System.out.println(myMagazine.PrintInformation());ArrayList<Publication>(); }//endnow of Main } //end of Class DriverOne |
Example output:
...
This Java code is a driver program that demonstrates how to use the TextBook
and Magazine
classes that extend the Publication
class.
Here's a detailed breakdown:
LocalDate
Import: This line imports theLocalDate
class from thejava.time
package.LocalDate
is a class that represents a date (year, month, day).main
Method: Themain
method is the entry point for any Java application. The Java Virtual Machine (JVM) calls this method when the program starts.Creating a
TextBook
Object: It creates a newTextBook
object namedmyTextBook
by calling theTextBook
constructor with the parameters"Learn Java"
,"Bart Simpson"
,759
,"8th Edition"
, and"Computer Science"
.Printing TextBook Information: The
System.out.println(myTextBook.PrintInformation());
statement prints the information of themyTextBook
object to the console by calling thePrintInformation
method of themyTextBook
object.Creating a
LocalDate
Object: It creates aLocalDate
object namedmyDate
representing the date August 30, 2022. This date object will be used as an argument while creating aMagazine
object.Creating a
Magazine
Object: It creates a newMagazine
object namedmyMagazine
by calling theMagazine
constructor with the parameters"Time Magazine"
,"Time Life Publisher"
,83
,"Monthly"
, andmyDate
.Printing Magazine Information: The
System.out.println(myMagazine.PrintInformation());
statement prints the information of themyMagazine
object to the console by calling thePrintInformation
method of themyMagazine
object.
...
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
|