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:

  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.

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