140 likes | 301 Views
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.
E N D
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 + Dog()+ Dog(String name)+ speak(): String
Subclass Bird Animal Bird + Bird()+ Bird(String name)+ speak(): String+ fly(): String
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");
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
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() );
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() );
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
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
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
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
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)
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