Polymorphism

Polymorphism is a core concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common base class. It enables a single interface to be used for objects of various types, providing flexibility and extensibility in software design.

Polymorphism allows methods to be written to accept objects of the base class, and those methods can be invoked with objects of any subclass that inherits from the base class. The actual behavior of the method is determined at runtime based on the type of the object that the method is called on, rather than the type of the reference or pointer used to invoke the method.

There are two main types of polymorphism:

  1. Compile-time polymorphism (static polymorphism): This type of polymorphism is achieved through method overloading and function templates. Method overloading allows multiple methods with the same name but different parameter lists to exist within a class. The appropriate method is selected at compile time based on the arguments used to call the method. Function templates, on the other hand, enable generic functions that can operate on different types based on the type specified during function invocation.

  2. Runtime polymorphism (dynamic polymorphism): This type of polymorphism is achieved through method overriding and virtual functions. Method overriding occurs when a derived class provides its own implementation of a method that is already defined in its base class. The appropriate method to be executed is determined at runtime based on the actual type of the object. Virtual functions are declared in the base class and marked as "virtual." They can be overridden by the derived classes, and the correct implementation is determined at runtime based on the object type. This is achieved through late binding or dynamic dispatch.

Polymorphism allows for code to be written in a more generic and flexible manner. It simplifies code maintenance, as new classes can be added without modifying existing code that operates on the base class. It also promotes code reuse, as common behavior can be defined in the base class and inherited by multiple subclasses.

In summary, polymorphism in OOP enables objects of different classes to be treated uniformly, providing flexibility, extensibility, and the ability to write more generic and reusable code. It is a powerful mechanism that enhances the modularity and maintainability of object-oriented systems.

Demo

Publication Base Class

package PolymorphismDemo; /** * @author Dr. Kevin Roark * An abstract Class for publications * */ abstract class Publication { //private Variables private String title; private String author; private int numberOfPages; protected String getTitle() { return title; } protected void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } protected void setAuthor(String author) { this.author = author; } protected int getNumberOfPages() { return numberOfPages; } protected 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; } abstract void WriteToFile(); } // end of Class

Interface - WriteToFile

package PolymorphismDemo; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; /** * @author Dr. Kevin Roark * interface to write a string to a text File */ public interface WriteToFile { //create file that will be in the same directory as the project files String fileName = "book.txt"; default void WriteRecordToFile(String myString) { try{ //create a FileWriter - set second argument to true so it will append or create if it does not exist FileWriter fw = new FileWriter(fileName, true); BufferedWriter bw = new BufferedWriter(fw); //write the argument to the file bw.write(myString); //add a newline to the file bw.newLine(); //now make sure you close the BufferWriter and the FileWriter bw.close(); fw.close(); } catch(IOException e) { System.out.println(e.getMessage() + "Error occured "); } } }

Inherited Classes

package PolymorphismDemo; /** * @author Dr. Kevin Roark * A class that inherits from Publication * */ public class TextBook extends Publication implements WriteToFile { private String version; private String subjectDomain; /** * @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; } /** * @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; } /** * Function to print the Information */ public String PrintInformation() { //First Call the super class version String myReturn = super.PrintInformation(); //Add this classes information myReturn += "Version: " + this.getVersion() + "\n"; myReturn += "Subject Domain: " + this.getSubjectDomain() + "\n"; return myReturn; } /** * Function to call the WriteTofile interface to write the Record to a file */ public void WriteToFile() { WriteRecordToFile(this.PrintInformation()); } }

Driver

Resulting book.txt file: