200 likes | 614 Views
Polymorphism. CSC 171 FALL 2004 LECTURE 15. Reading. Read Chapter 9 of Horstmann. Polymorphism. Poly – “many” Morphism – “form”. Polymorphism. Polymorphism in JAVA is the idea that the behavior of an object can vary based on it’s type. Similar Idea. “Overloading” a method.
E N D
Polymorphism CSC 171 FALL 2004 LECTURE 15
Reading • Read Chapter 9 of Horstmann
Polymorphism • Poly – “many” • Morphism – “form”
Polymorphism • Polymorphism in JAVA is the idea that the behavior of an object can vary based on it’s type.
Similar Idea • “Overloading” a method. • We don’t just have one possible “add” method
public static Rational add(Rational r1, Rational r2) { int newNum = r1.numerator * r2.denominator + r1.denominator * r2.numerator ; int newDenom = r1.denominator * r2.denominator; return new Rational(newNum,newDenom); } public Rational add(Rational r) { int newNum = this.numerator * r.denominator + r.denominator * this.numerator ; int newDenom = this.denominator * r.denominator; return new Rational(newNum,newDenom); }
Key Difference • In general, we have multiple ways of doing something. The machine must decide on which to use. • With overloading, this decision is made at compile time (early binding) • With polymorphism, this decision is made at run time (late binding).
Excercise • Let’s say we want to have a computer program write stories for us • Elisa • Boris
Example - exercise Both freshmen and seniors are “greeters”. When asked to greet, freshmen say “Ummm . . Like . . Yo!” When asked to greet, seniors say “Hello, and how are you this fine day?”
Exercise Write a method “greeting” that takes an object as a parameter. Have objects of the “Freshman” type return the string “Ummm . . Like . . Yo!” Have objects of the “Senior” type return the string “Hello, and how are you this fine day?”
public String greeting (Object o) { if (o instanceof Freshman) return “Ummm . . Like . . Yo!”; if (o instanceof Freshman) return “Hello, and how are you this fine day?” return “”; }
Example - exercise • Define a “Greeter” interface (based on the defs of the previous lecture). • Anything that implements “Greeter” must have a parameterless “greeting” method that returns a string.
public interface Greeter { public String greeting () ; }
Example - exercise Both freshmen and seniors are “greeters”. When asked to greet, freshmen say “Ummm . . Like . . Yo!” When asked to greet, seniors say “Hello, and how are you this fine day?”
public class Freshmen implements Greeter { public String greeting() { return “Ummm . . Like . . Yo!”; } } public class Senior implements Greeter { public String greeting() { return “Hello, and how are you this fine day?” ; } }
Why is this useful We can write code that works the same way for a lot of different types of objects. Even though the objects behave differently. Suppose we are writing a program that automatically generates stories. A common phrase involves a greeting followed by “, the person said”.
Write a JAVA method that takes a “Greeter” object and returns a string with the greeting followed by “he said”.
public String sentence(Greeter g) { return g.greeting() + “, he said.”; }
Using it in context public static void main(String args) { Freshmen ted = new Freshman: Senior bill = new Senior; System.out.println(“Ted looked at Bill. “ + sentence(ted)); System.out.println(“Bill regarded Ted. “ + sentence(bill)); }
We can accomplish the same thing (different behavior) with program logic instead of polymorphism • However, the polymorphic solution allows us to reduce program complexity and encapsulate data. • This reduces the potential for faults • This allows us to add new types later.