Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Our base class (super class - Publication)

/**
 * A general Class for publications - our base class
 */
public class Publication {
	//private Variables
	private String title;
	private String author;
	private int numberOfPages;
	
	/* Getters and Setters */
	public String getTitle() {
		return title;
	}
	protected void setTitle(String title) {
		this.title = title;
	}
	protected  String getAuthor() {
		return author;
	}
	protected void setAuthor(String author) {
		this.author = author;
	}
	public int getNumberOfPages() {
		return numberOfPages;
	}
	public void setNumberOfPages(int numberOfPages) {
		this.numberOfPages = numberOfPages;
	}
	
	/* Constructors */
	protected Publication(String title, String author, int numberOfPages) {
		super();
		this.title = title;
		this.author = author;
		this.numberOfPages = numberOfPages;
	}
	
	/* Class Methods */
	protected String PrintInformation()
	{
		String myReturn = "";
		myReturn += "Title: " + this.getTitle() + "\n";
		myReturn += "Author: " + this.getAuthor() + "\n";
		myReturn += "Number of Pages: " + this.getNumberOfPages() + "\n";		
		return myReturn; 
	}
} // end of Class

This Java code defines a class named Publication. This class is used to represent a general publication. It contains several private variables, getters and setters for those variables, a constructor, and a class method.

Here is a detailed breakdown:

  1. Private Variables: These are the instance variables for the Publication class. They hold the state of an object created from the class. In this case, each publication has a title (a string), an author (another string), and a numberOfPages (an integer).

  2. Getters and Setters: These are methods that allow external code to safely interact with the instance variables. The getTitle, getAuthor, and getNumberOfPages methods return the values of the corresponding instance variables. The setTitle, setAuthor, and setNumberOfPages methods allow you to set the values of the corresponding instance variables. Notice that the setters and getters for title and author are protected, which means they can only be accessed within the same package and by subclasses. The numberOfPages setter and getter are public, so they can be accessed from anywhere.

  3. Constructor: The Publication constructor is a method that gets called when a new instance of the class is created. It sets the initial state of the object by initializing the instance variables. In this case, it takes a title, author, and numberOfPages as parameters and assigns these values to the instance variables.

  4. Class Method (PrintInformation): This protected method constructs and returns a string containing the title, author, and number of pages of the publication. The string is formatted with each piece of information on a new line. Protected methods, like protected instance variables, can only be accessed within the same package and by subclasses.

This class acts as a base class that could be extended by other classes that represent more specific types of publications, such as books, magazines, or newspapers. Each specific type of publication might have additional instance variables (like an ISBN for a book) and additional methods.

Derived Class - Magazine

/**
 * A derived class from Publications
 */
import java.time.LocalDate;

public class Magazine extends Publication {
	//variables for Magazine
	private String subscriptionType;
	private LocalDate subscritionDate;
	
	//Getters and Setters 
	/**
	 * @return the subscriptionType
	 */
	public String getSubscriptionType() {
		return subscriptionType;
	}
	/**
	 * @param subscriptionType the subscriptionType to set
	 */
	public void setSubscriptionType(String subscriptionType) {
		this.subscriptionType = subscriptionType;
	}
	/**
	 * @return the subscritionDate
	 */
	public LocalDate getSubscritionDate() {
		return subscritionDate;
	}
	/**
	 * @param subscritionDate the subscritionDate to set
	 */	
	public void setSubscritionDate(LocalDate subscritionDate) {
		this.subscritionDate = subscritionDate;
	}
	
	//Constructor
	/**
	 * @param title
	 * @param author
	 * @param numberOfPages
	 * @param subscriptionType
	 * @param subscritionDate
	 */
	public Magazine(String title, String author, int numberOfPages, String subscriptionType, LocalDate subscritionDate) {
		super(title, author, numberOfPages);
		this.subscriptionType = subscriptionType;
		this.subscritionDate = subscritionDate;
	}
	
	//Methods 
	public String PrintInformation()
	{
		String myReturn = super.PrintInformation();
		myReturn += "Subscription Type: " + getSubscriptionType() + "\n";
		myReturn += "Publish Date: " + this.getSubscritionDate().toString() + "\n";
		return myReturn; 
	}
}

This Java code defines a class named Magazine, which extends the Publication class. This means Magazine is a subclass of Publication, and it inherits all the fields and methods from the Publication class. However, the Magazine class also adds some new fields and methods of its own. This is a fundamental aspect of object-oriented programming known as inheritance.

Here is a detailed breakdown:

  1. Additional Private Variables: In addition to the private variables inherited from the Publication class (title, author, and numberOfPages), the Magazine class has two new private variables: subscriptionType (a string) and subscriptionDate (a LocalDate).

  2. Getters and Setters: The getSubscriptionType, setSubscriptionType, getSubscriptionDate, and setSubscriptionDate methods are getters and setters for the subscriptionType and subscriptionDate fields, allowing external code to safely interact with these fields.

  3. Constructor: The Magazine constructor calls the super constructor (which is the constructor of the superclass, Publication) to initialize the title, author, and numberOfPages fields. Then, it initializes the subscriptionType and subscriptionDate fields.

  4. Overridden Method (PrintInformation): The PrintInformation method is overridden in the Magazine class. This means that although a PrintInformation method was defined in the superclass (Publication), a new definition is provided in the subclass (Magazine). This new definition first calls the PrintInformation method of the superclass to get a string with the publication's general information. Then, it adds information about the subscription type and subscription date. This demonstrates another key aspect of object-oriented programming: method overriding.

So, this Magazine class is a more specific type of Publication that includes additional information relevant to magazines (namely, the subscription type and date). This follows the principle of "is-a" relationship in object-oriented programming, which in this case means "a magazine is a publication".

Derived class TextBook

/**
 * A Textbook class that inherits from Publications - our base class
 */
public class TextBook extends Publication{
	private String version;
	private String subjectDomain;
	
	//Getters and Setters 
	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}
	/**
	 * @param version the version to set
	 */
	public void setVersion(String version) {
		this.version = version;
	}
	/**
	 * @return the subjectDomain
	 */
	public String getSubjectDomain() {
		return subjectDomain;
	}
	/**
	 * @param subjectDomain the subjectDomain to set
	 */
	public void setSubjectDomain(String subjectDomain) {
		this.subjectDomain = subjectDomain;
	}
	
	//Constructors 
	/**
	 * @param title
	 * @param author
	 * @param numberOfPages
	 * @param version
	 * @param subjectDomain
	 */
	public TextBook(String title, String author, int numberOfPages, String version, String subjectDomain) {
		super(title, author, numberOfPages);
		this.version = version;
		this.subjectDomain = subjectDomain;
	}
	
	//Functions / Methods 
	public String PrintInformation()
	{
		String myReturn = super.PrintInformation();
		myReturn += "Version: " + this.getVersion() + "\n";
		myReturn += "Subject Domain: " + this.getSubjectDomain() + "\n";		
		return myReturn; 
	}

}

This Java code defines a class called TextBook that extends the Publication class. This means that TextBook is a subclass of Publication, and it inherits all the fields and methods from Publication. TextBook also adds its own fields and methods specific to textbooks.

Here's a detailed breakdown:

  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 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 (Demo1)

/**
 * Driver class to test Textbook and Magazine Class that inherit from Publication 
 */
import java.time.LocalDate; //needed library for LocalDate Class

public class Demo1 {

	public static void main(String[] args) {
		//create an instance of Textbook
		TextBook myTextBook = new TextBook("Learn Java", "Bart Simpson", 759, "8th Edition", "Computer Science");
		System.out.println(myTextBook.PrintInformation());	
		
		//create a date that will be used for the Magazine 
		LocalDate myDate = LocalDate.of(2022, 8, 30);   
		
		//create an instance of Magazine 
		Magazine myMagazine = new Magazine("Time Magazine", "Time Life Publisher", 83, "Monthly", myDate);
		System.out.println(myMagazine.PrintInformation());		
		
	}//end 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:

  1. LocalDate Import: This line imports the LocalDate class from the java.time package. LocalDate is a class that represents a date (year, month, day).

  2. main Method: The main method is the entry point for any Java application. The Java Virtual Machine (JVM) calls this method when the program starts.

  3. Creating a TextBook Object: It creates a new TextBook object named myTextBook by calling the TextBook constructor with the parameters "Learn Java", "Bart Simpson", 759, "8th Edition", and "Computer Science".

  4. Printing TextBook Information: The System.out.println(myTextBook.PrintInformation()); statement prints the information of the myTextBook object to the console by calling the PrintInformation method of the myTextBook object.

  5. Creating a LocalDate Object: It creates a LocalDate object named myDate representing the date August 30, 2022. This date object will be used as an argument while creating a Magazine object.

  6. Creating a Magazine Object: It creates a new Magazine object named myMagazine by calling the Magazine constructor with the parameters "Time Magazine", "Time Life Publisher", 83, "Monthly", and myDate.

  7. Printing Magazine Information: The System.out.println(myMagazine.PrintInformation()); statement prints the information of the myMagazine object to the console by calling the PrintInformation method of the myMagazine object.

In summary, this program demonstrates the use of polymorphism and inheritance in Java. TextBook and Magazine are both subclasses of Publication, but they add additional properties and behaviors specific to their type, as can be seen in the PrintInformation method and the data members of each class. The driver program then creates objects of each type and prints out their information.

  • No labels