Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

The term polymorphism can be defined as “having many forms.” A polymorphic reference is a reference variable that can refer to different types of objects at different points in time. The specific method invoked through a polymorphic reference can change from one invocation to the next.

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

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

/* 
 * Polymorphism  Late binding 
 */

// Animal class - this is the base class 
 class Animal {	 
  public void animalSound()
  {
	  System.out.println("The animal makes a sound");
  } 
   
  // Regular method
  public void sleep() {
    System.out.println("Zzz");
  }
} //end of Animal Base Class
 
 /* *********************************
  * Derived Classes
  *********************************** */

//Subclass (inherit from Animal)
//Notice how we define the Abstract method animalSound declared in the super class 
class Cat extends Animal {
	public void animalSound() {
	// The body of animalSound() is provided here
	System.out.println("The cat says Meow");
	}
} //end of Cat Class

//Subclass (inherit from Animal)
class Dog extends Animal {
	public void animalSound() {
	// The body of animalSound() is provided here
	System.out.println("The dog says Woof");
	}
} //end of Dog class

//Subclass (inherit from Animal)
class Cow extends Animal {
	public void animalSound() {
	// The body of animalSound() is provided here
	System.out.println("The dog says Moo");
	}
} 


/*
 * ******************************
 * Main Program to demo polymorphism
 * Author Dr. Kevin Roark
 * ******************************
 */

public class Demo5 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 Animal myAnimal = new Animal();  // Create a Animal object
		 myAnimal.animalSound();
		 myAnimal.sleep();
		 
		 //now create an Animal and make it a Dog
		 Animal myDog = new Dog();
		 myDog.animalSound();
		 myDog.sleep();
		 
		 //now create an Animal and make it a Cat
		 Animal myCat = new Cat();
		 myCat.animalSound();
		 myCat.sleep();
		 
		 Animal myCow = new Cow();
		 myCow.animalSound();
		 myCow.sleep();
		 
		 //demonstration of instanceof
		 if(myCat instanceof Cat)
		 {
			 System.out.println("This is a cat!");
		 }
		 if(myCat instanceof Animal)
		 {
			 System.out.println("This is a Animal!");
		 }

	}

}

The given Java code is a demonstration of polymorphism in Java using animal sounds as examples. Polymorphism allows objects to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Let's go through the important parts:

  1. The Animal class is the parent (base) class with two methods: animalSound() and sleep(). animalSound() is overridden in each of the child classes while sleep() is not, meaning it's the same across all animal types.

  2. Cat, Dog, and Cow are subclasses that each extend Animal. They each override the animalSound() method to print out a unique sound for that animal. This is a perfect example of method overriding in Java, which is one of the ways we can achieve polymorphism.

  3. In the main method, we first create a generic Animal object and call its methods. This is standard object-oriented programming.

  4. Then we create Dog, Cat, and Cow objects but refer to them with Animal references. This is polymorphism in action - we can call the animalSound() method on these references and it will call the overridden method in the respective subclass, even though the reference type is Animal. This is known as late binding or dynamic method dispatch.

  5. The sleep() method is also called on these references. Since sleep() is not overridden in the subclasses, the original sleep() method from Animal class is called.

  6. Finally, the instanceof keyword is used to check if myCat is an instance of Cat and Animal. instanceof returns true if the object being compared is an instance of the specified type or an instance of a subclass of the specified type. So, in this case, both checks will return true because myCat is a Cat object and Cat is a subclass of Animal. This is why "This is a cat!" and "This is a Animal!" both get printed.

  • No labels