...
At some point, the commitment is made to execute certain code to carry out a method invocation. This commitment is referred to as binding a method invocation to a method definition. In many situations, the binding of a method invocation to a method definition can occur at compile time. For polymorphic references, however, the decision cannot be made until run time. The method definition that is used is based on the object that is being referred to by the reference variable at that moment. This deferred commitment is called late binding or dynamic binding. It is less efficient than binding at compile time, because the decision must be made during the execution of the program. This overhead is generally acceptable in light of the flexibility that a polymorphic reference provides.
Code Example
Using the classes we have created: Publication, Magazine, and Textbook → Demo 2 Inheritance
Code Block | ||
---|---|---|
| ||
/* 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 Demo4 {
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 Magizine", "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
|
The given Java code is a demonstration of Polymorphism using an ArrayList of objects that belong to the Publication
parent class and its child classes TextBook
and Magazine
.
ArrayList<Publication> myPublications = new ArrayList<Publication>();
: This line creates an ArrayList namedmyPublications
which can hold objects of typePublication
. BecauseTextBook
andMagazine
are both subclasses ofPublication
, they can also be added to this ArrayList due to polymorphism.The code then adds instances of
TextBook
andMagazine
to themyPublications
ArrayList using theadd()
method.myPublications.add(new TextBook("Learn Java", "Bart Simpson", 759, "8th Edition", "Computer Science"));
This line creates a newTextBook
object with given values and adds it to themyPublications
ArrayList.LocalDate myDate = LocalDate.of(2022, 8, 30);
This line creates a newLocalDate
object namedmyDate
.myPublications.add(new Magazine("Time Magazine", "Time Life Publisher", 83, "Monthly", myDate));
This line creates a newMagazine
object with given values and adds it to themyPublications
ArrayList.
The code then uses a for-each loop to iterate over the
myPublications
ArrayList. Each time through the loop, it calls thePrintInformation()
method on the current object.for(Publication pPubs : myPublications) { System.out.println(pPubs.PrintInformation()); }
This is the for-each loop that iterates through eachPublication
object inmyPublications
.pPubs
holds the currentPublication
object during each iteration. ThePrintInformation()
method is then called onpPubs
and its return value is printed to the console.
This demonstrates polymorphism because the PrintInformation()
method that gets called depends on whether pPubs
is a TextBook
or Magazine
object. This decision is made at runtime based on the actual type of the object, even though pPubs
is declared as a Publication
(the parent class of both TextBook
and Magazine
).
...
Additionally, we will demonstrate the use of the instance of operator
...