1 / 20

Polymorphism

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.

keren
Download Presentation

Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Polymorphism CSC 171 FALL 2004 LECTURE 15

  2. Reading • Read Chapter 9 of Horstmann

  3. Polymorphism • Poly – “many” • Morphism – “form”

  4. Polymorphism • Polymorphism in JAVA is the idea that the behavior of an object can vary based on it’s type.

  5. Similar Idea • “Overloading” a method. • We don’t just have one possible “add” method

  6. 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); }

  7. 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).

  8. Excercise • Let’s say we want to have a computer program write stories for us • Elisa • Boris

  9. 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?”

  10. 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?”

  11. 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 “”; }

  12. 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.

  13. public interface Greeter { public String greeting () ; }

  14. 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?”

  15. 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?” ; } }

  16. 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”.

  17. Write a JAVA method that takes a “Greeter” object and returns a string with the greeting followed by “he said”.

  18. public String sentence(Greeter g) { return g.greeting() + “, he said.”; }

  19. 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)); }

  20. 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.

More Related