Freitag, 12. Dezember 2008

EE3206 Java Polymorphism

EE3206/EE5805 Java programming and Applictions

notes on Savitch(2006)'s Absolute Java , p 478-486

1. Polymorphism refers to the ability to associate many meanings to one method name by means of the late binding mechanism.

2. with late binding, the definition of a method is not bound to an invoction of the method until run time, in fact, not until th time at which the particular invocation takes place.


//Sales.java
//superclass
public class Sale
{
private String name;
private double price;

public Sale()
{
name = "No name yet";
price =0;
}

public Sale(String theName, double thePrice)
{
setName(theName);
setPrice(thePrice);
}

public Sale(Sale originalObject)
{
if(originalObject == null)
{
System.out.print("Error: null Saleobject.");
System.exit(0);

}
name = originalObject.name;
price = originalObject.price;

}
public static void announcement()
{
System.out.println("this is the Sale class");
}

public double getPrice()
{
return price;
}
public void setPrice(double newPrice)
{
if(newPrice >= 0)
price = newPrice;
else
{
System.out.println("Error: Negative price.");
System.exit(0);
}
}
public String getName()
{
return name;
}
public void setName(String newName)
{
if (newName != null && newName !="")
name = newName;
else
{
System.out.println("Error: Improper name value.");
System.exit(0);

}


}
public String toString()
{
return (name + " Price and TTotal Cost = $" + price);
}
public double bill()
{
return price;
}

/* returns true if the names are the same and the
bill for the calling object is equal to the bill for otherSale; */

public boolean equalDeals(Sale otherSale)
{
if(otherSale == null)
return false;
else
return (name.equals(otherSale.name)
&& bill() == otherSale.bill() );
}

/* returns true if the bill for the calling
object is less than the bill for otherSale */

public boolean lessThan(Sale otherSale)
{
if(otherSale == null)
{
System.out.println("Error: null Sale object");
System.exit(0);

}
return (bill() < otherSale.bill() );

}
public boolean equals(Object otherObject)
{
if (otherObject == null)
return false;
else if (getClass() != otherObject.getClass())
return false;
else
{
Sale otherSale = (Sale)otherObject;

return(name.equals(otherSale.name)
&& (price == otherSale.price));
}
}

}


//DiscountSale.java
//derived class, note the method name
public class DiscountSale extends Sale
{
private double discount;
//cannot be negative

public DiscountSale()
{
super();
discount = 0;

}
public DiscountSale(String theName,
double thePrice, double theDiscount)

{
super(theName, thePrice);
setDiscount(theDiscount);

}
public DiscountSale(DiscountSale originalObject)
{
super(originalObject);
discount = originalObject.discount;

}
public static void announcement()
{
System.out.println("This is theDiscountSale class.");
}
public double bill()
{
double fraction = discount/100;

return (1-fraction) *getPrice();
}
public double getDiscount()
{
return discount;
}

public void setDiscount(double newDiscount)
{
if (newDiscount >= 0)
discount = newDiscount;
else
{
System.out.println("Error: Negative discount.");
System.exit(0);

}
}
public String toString()
{
return (getName() + " Price = $" + getPrice()
+ " Discount = " + discount
+"%\n" + " Total cost = $"
+ bill());
}
public boolean equals(Object otherObject)
{
if (otherObject == null)
return false;
else if (getClass() != otherObject.getClass())
return false;
else
{
DiscountSale otherDiscountSale =
(DiscountSale)otherObject;

return(super.equals(otherDiscountSale)
&& discount == otherDiscountSale.discount);

}
}


}


//LateBindingDemo.java : main
public class LateBindingDemo
{
public static void main(String[] args)
{
Sale simple = new Sale("floor mat", 10.00);
DiscountSale discount = new DiscountSale("floor mat", 11.00, 10);

System.out.println(simple);
System.out.println(discount);

if (discount.lessThan(simple))
System.out.println("Discounted item is cheaper");
else
System.out.println("Discount item is not cheaper.");

Sale regularPrice = new Sale("cup holder", 9.99);
DiscountSale specialPrice =
new DiscountSale("cup holder", 11.00, 10);

System.out.println(regularPrice);
System.out.println(specialPrice);

if (specialPrice.equalDeals(regularPrice))
System.out.println("Deals are equal.");
else
System.out.println("Deals are not equal.");


}

}




Keine Kommentare: