14
Polymorphism Illustration John Lamertina

Polymorphism Illustration

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Polymorphism Illustration

Polymorphism Illustration

John Lamertina

Page 2: Polymorphism Illustration

Superclass Animal

Animal

- name

+ Animal()+ Animal(String name)

+ getName(): String+ setname(String name)

+ toString(): String+ speak(): String

Italics indicate abstract

Page 3: Polymorphism Illustration

Subclass Dog

Dog

+ Dog()+ Dog(String name)

+ speak(): String

Animal

Page 4: Polymorphism Illustration

Subclass Bird

Bird

+ Bird()+ Bird(String name)

+ speak(): String+ fly(): String

Animal

Page 5: Polymorphism Illustration

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");

Page 6: Polymorphism Illustration

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

Page 7: Polymorphism Illustration

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() );

Page 8: Polymorphism Illustration

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() );

Page 9: Polymorphism Illustration

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

Page 10: Polymorphism Illustration

Polymorphism

Using a superclass variable to reference subclass instances, and invoking the correct subclass version of an overridden methodSuperclass Animal variable Invoke correct subclass version of speak

method for Cat, Dog, Bird, and other instances

Page 11: Polymorphism Illustration

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

Page 12: Polymorphism Illustration

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

Page 13: Polymorphism Illustration

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)

Page 14: Polymorphism Illustration

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