Demo - Polymorphism - Publication, Textbook, Magazine
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:
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 atitle
(a string), anauthor
(another string), and anumberOfPages
(an integer).Getters and Setters: These are methods that allow external code to safely interact with the instance variables. The
getTitle
,getAuthor
, andgetNumberOfPages
methods return the values of the corresponding instance variables. ThesetTitle
,setAuthor
, andsetNumberOfPages
methods allow you to set the values of the corresponding instance variables. Notice that the setters and getters fortitle
andauthor
areprotected
, which means they can only be accessed within the same package and by subclasses. ThenumberOfPages
setter and getter arepublic
, so they can be accessed from anywhere.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 atitle
,author
, andnumberOfPages
as parameters and assigns these values to the instance variables.Class Method (
PrintInformation
): Thisprotected
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, likeprotected
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:
Additional Private Variables: In addition to the private variables inherited from the
Publication
class (title
,author
, andnumberOfPages
), theMagazine
class has two new private variables:subscriptionType
(a string) andsubscriptionDate
(aLocalDate
).Getters and Setters: The
getSubscriptionType
,setSubscriptionType
,getSubscriptionDate
, andsetSubscriptionDate
methods are getters and setters for thesubscriptionType
andsubscriptionDate
fields, allowing external code to safely interact with these fields.Constructor: The
Magazine
constructor calls thesuper
constructor (which is the constructor of the superclass,Publication
) to initialize thetitle
,author
, andnumberOfPages
fields. Then, it initializes thesubscriptionType
andsubscriptionDate
fields.Overridden Method (
PrintInformation
): ThePrintInformation
method is overridden in theMagazine
class. This means that although aPrintInformation
method was defined in the superclass (Publication
), a new definition is provided in the subclass (Magazine
). This new definition first calls thePrintInformation
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:
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.
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 (Demo)
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.
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark