Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
languagejava
/* 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.

  1. ArrayList<Publication> myPublications = new ArrayList<Publication>(); : This line creates an ArrayList named myPublications which can hold objects of type Publication. Because TextBook and Magazine are both subclasses of Publication, they can also be added to this ArrayList due to polymorphism.

  2. The code then adds instances of TextBook and Magazine to the myPublications ArrayList using the add() method.

    • myPublications.add(new TextBook("Learn Java", "Bart Simpson", 759, "8th Edition", "Computer Science")); This line creates a new TextBook object with given values and adds it to the myPublications ArrayList.

    • LocalDate myDate = LocalDate.of(2022, 8, 30); This line creates a new LocalDate object named myDate.

    • myPublications.add(new Magazine("Time Magazine", "Time Life Publisher", 83, "Monthly", myDate)); This line creates a new Magazine object with given values and adds it to the myPublications ArrayList.

  3. The code then uses a for-each loop to iterate over the myPublications ArrayList. Each time through the loop, it calls the PrintInformation() method on the current object.

    • for(Publication pPubs : myPublications) { System.out.println(pPubs.PrintInformation()); } This is the for-each loop that iterates through each Publication object in myPublications. pPubs holds the current Publication object during each iteration. The PrintInformation() method is then called on pPubs 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

...