CSC 216/s08/strive for happiness
Formatting Resources
Formatting Help Guide from MetaWiki
What is Polymorphism?
The problem
Polymorphism is derived from Greek, meaning "many forms". In Java, polymorphism is using a superclass variable to refer to a subclass object, the "many forms" of the superclass variable.
The core concepts that this activity will teach are polymorphism and inheritance. It will explain why inheritance is an "is-a" relationship between two classes, and how variables of a given type can refer to objects of either their own class, or any subclass object. It will also touch on how methods are overridden by subclass methods when they are defined in both the superclass and subclass.
Participants and props
The class will be divided into small groups of 3-5 people. Each group will receive a number of Animal Cards (explained below) and a list of exercises will be shown on the board.
An Action Card will be a small index card with a class definition and one or two methods. For example:
public class Dog extends Quadruped { public void talk() { System.out.println("Woof"); } }
public class Quadruped extends Animal { public void walk() { System.out.println("Walk"); } }
public class Animal { }
public class Bird extends Animal { public void fly() { System.out.println("Fly"); } public void talk() { System.out.println("Chirp"); } }
public class Owl extends Bird { public void talk() { System.out.println("Whoo"); } }
An html page, powerpoint slide, or simply a part of the class notes will have a list of exercises for the groups to perform. For example:
Bird B = new Owl(); B.talk();
Animal A = new Bird(); A.talk();
A few, more advanced exercises would be added at the end, to show off the real power of polymorphism, such as:
Animal[] A = new Animal[3]; A[0] = new Owl(); A[1] = new Dog(); A[2] = new Cow(); for(Animal a: A) a.talk();
The students will then write down the answers to all of these exercises based upon what is on their cards.
The script
The exercise will be introduced, and the props and instructions given to the class, which should take no more than 5 minutes. Each group will be given a stack of about 15 cards, and each group will have identical cards to each other group.
The class will then be given 10-15 minutes to do about 20 exercises, based on the cards they are given.
After the exercise is concluded, the class will review the correct answers and discuss them for 10-15 minutes. The class will go over how the Animal superclass is analogous to the Object superclass in Java. They will also talk about how methods of a subclass override methods of the superclass, in the same way that the methods of the animals do.
--Deswartz 17:37, 26 March 2008 (EDT)