Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Private Variables: In addition to the private variables inherited from the Publication class (title, author, numberOfPages), the TextBook class introduces two additional private variables: version (a string that represents the version of the textbook) and subjectDomain (a string that represents the textbook's subject area).

  2. Getters and Setters: These methods are used to get (getVersion and getSubjectDomain) and set (setVersion and setSubjectDomain) the values of the version and subjectDomain variables.

  3. Constructor: The constructor for the TextBook class (which takes five parameters: title, author, numberOfPages, version, and subjectDomain) first calls the Publication constructor (using the super keyword) to initialize the title, author, and numberOfPages fields inherited from Publication. Then it sets the version and subjectDomain fields with the provided values.

  4. Overridden Method: The PrintInformation method is overridden in TextBook. This method first calls the superclass's PrintInformation method to get a string of the general publication's information. Then it adds information about the textbook's version and subjectDomain. 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
languagejava
/* 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 

...