1 / 14

Polymorphism Illustration

Polymorphism Illustration. John Lamertina. Superclass Animal. Animal. - name. + Animal() + Animal(String name) + getName(): String + setname(String name) + toString(): String + speak(): String. Italics indicate abstract. Subclass Dog. Animal. Dog.

Télécharger la présentation

Polymorphism Illustration

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 Illustration John Lamertina

  2. Superclass Animal Animal - name + Animal()+ Animal(String name)+ getName(): String+ setname(String name)+ toString(): String+ speak(): String Italics indicate abstract

  3. Subclass Dog Animal Dog + Dog()+ Dog(String name)+ speak(): String

  4. Subclass Bird Animal Bird + Bird()+ Bird(String name)+ speak(): String+ fly(): String

  5. Driver. part 1 // Polymorphism: Use a superclass variable to reference subclass objects //Create a superclass array. Assign to each element a subclass instance Animal a[] = new Animal[6]; a[0] = new Dog("Boomer"); a[1] = new Dog("Champ"); a[2] = new Dog("Dodger"); a[3] = new Cat("Elsa"); a[4] = new Bird("Fog Horn"); a[5] = new Cat("Garfield");

  6. Driver. part 2 // Polymorphism: use superclass variable to invoke subclass methods. // Superclass variable can only invoke methods declared in the superclass) for (Animal animal : a) System.out.printf ( "%s%s%s\n",animal, " says ", animal.speak() ); // This works because Java uses “dynamic binding” (i.e. at run time) to determine the type of object referenced

  7. Driver. part 3 // To invoke a method that only appears in a subclass, we must: // 1. Determine the subclass (instanceof) // 2. Downcast from the superclass to the subclass // 3. Execute the subclass method for (Animal animal : a) { if (animal instanceof Bird) { Bird b = (Bird) animal; System.out.printf("%s%s%s\n",animal," flies: ", b.fly() );

  8. Driver. part 4 // To determine the class of any object for (Animal animal : a) System.out.printf ( "%s%s%s\n", animal, " is a ", animal.getClass().getName() );

  9. Abstract Classes • Purpose: provide an appropriate superclass from which subclasses can share a common design • Contains one or more abstract methods • Cannot declare instances of an abstract class, but can declare variables that reference objects of a concrete subclass • Facilitate polymorphism

  10. Polymorphism • Using a superclass variable to reference subclass instances, and invoking the correct subclass version of an overridden method • Superclass Animal variable • Invoke correct subclass version of speak method for Cat, Dog, Bird, and other instances

  11. Polymorphism (continued) • JVM determines (at run-time) which subclass version of a method to call. This is known as “dynamic-binding”. • Superclass reference variable may only invoke methods declared in the superclass

  12. Interface: two similar definitions • Logical Interface: Set of public methods that clients (other programs) use to interact with an object. Establishes what operations a object can perform. • Java Interface: Set of common methods and constants that define the actions or values that are used by unrelated classes. • Standardizes operations • Allows unrelated objects to be called polymorphically

  13. Class Organization • Compositional: Class is comprised of other Classes (has-a relationship) • Hierarchical: Subclass extends Class through inheritance (is-a relationship). • Functional: Class implements Interfaces (act-as relationship)

  14. Java Interface • A named block of statements, containing only constants and abstract methods • All members are public • Methods implicitly public and abstract • Fields implicitly public static final • No implementation code • No instance variables

More Related