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.
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:
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.
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.