D.2.3 Define the term polymorphism.
"The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language."
POLYMORPHISM is a principle where subclasses of a class can define their own unique behaviours and yet share some of the same functionality of the super class.
OVERRIDING is writing a new instance method in the subclass that has the same signature as the one in the superclass. Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes Overriding is a type of polymorphism along with overloading and dynamic (late) binding.
METHOD|CONSTRUCTOR OVERLOADING - polymorphism is achieved through method overloading. Method overloading means there are several methods present in a class having the same name but different types/order/number of parameters.
Method overloading is in the same class, where more than one method have the same name but different signatures.
Ex:
void sum (int a , int b);
void sum (int a , int b, int c);
void sum (float a, double b);
Method overriding is when one of the methods in the super class is redefined in the sub-class. In this case, the signature of the method remains the same.
Ex:
class X{
public int sum(){
// some code
}
}
class Y extends X{
public int sum(){
//overridden method
//signature is same
}
}
Polymorphism can be demonstrated with the following superclass and subclasses:
I. Note the getSellingPrice() method of this superclass
/**
*
* @author andersonm
*/
public class Item {
double taxRate;
double originalPrice;
public Item(double taxRate, double originalPrice) {
this.taxRate = taxRate;
this.originalPrice = originalPrice;
}
public Item() {
taxRate = 0;
originalPrice = 0.0;
}
public void setTaxRate(double taxRate) {
this.taxRate = taxRate;
}
public void setOriginalPrice(double originalPrice) {
this.originalPrice = originalPrice;
}
public double getTaxRate() {
return taxRate;
}
public double getOriginalPrice() {
return originalPrice;
}
public double getSellingPrice(){
double sellingPrice = originalPrice + (originalPrice * taxRate);
return sellingPrice;
}
@Override
public String toString() {
return "Item{" + "taxRate=" + taxRate + ", originalPrice=" + originalPrice + '}';
}
}
II. Note the getSellingPrice() method of this subclass that overrides the same method in the superclass
/**
*
* @author andersonm
*/
public class NonDiscountedNonTaxableItem extends Item {
@Override
public double getSellingPrice(){
return super.getOriginalPrice();
}
public String toString() {
return "NonTaxableItem{" + "original Price=" + super.toString() + '}';
}
}
III. Note the getSellingPrice() method of this other subclass that overrides the same method in the superclass
/**
*
* @author andersonm
*/
public class DiscountedAndTaxableItem extends Item {
private double discount;
public DiscountedAndTaxableItem(double discount, double taxRate, double originalPrice) {
super(taxRate, originalPrice);
this.discount = discount;
}
public DiscountedAndTaxableItem(double discount) {
this.discount = discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
public double getDiscount() {
return discount;
}
public double getSellingPrice(){
return super.getSellingPrice() - discount;
}
@Override
public String toString() {
return "TaxableItem{" + "discount=" + discount + '}';
}
}
In summary, there are 3 classes: Item, NonDiscountedNonTaxableItem, and DiscountedAndTaxableItem. The two subclasses override the getSellingPrice() method and returns unique information.
"The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable's type (in the following example, the variable type is Item and the objects that these variables referred to areItem, NonDiscountedNonTaxableItem, and DiscountedAndTaxableItem). This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language."
Using the following client code, each variable calls the appropriate method for the object:
public class TestItems {
public static void main(String[] args) {
Item i1, i2, i3;
i1 = new Item(0.10, 100.00);
i2 = new DiscountedAndTaxableItem(0.10, 0.10, 150.00);
i3 = new NonDiscountedNonTaxableItem(0.10, 120.00);
System.out.println("Selling Price i1: "+ i1.getSellingPrice());
System.out.println("Selling Price i2: " + i2.getSellingPrice());
System.out.println("Selling Price i3: " + i3.getSellingPrice());
}
}
OUTPUT:
Selling Price i1: 110.0
Selling Price i2: 164.9
Selling Price i3: 120.0