CS 18000 Lecture 14: Polymorphism
Document Summary
Many forms : animals can take on many forms yet exhibit similar behaviors. Java allows a superclass variable to contain a reference to a subclass object: the compiler chooses the subclass implementation of any overridden methods, account. withdraw(amount); could be savings acct, checking acct, money market acct, . Horse: lots of redundancy, so you create a superclass. A: nothing--don"t want to create a generic animal. Animal public abstract class animal { abstract void speak(); public static void main(string[] args) { Animal[] animals = new animal[2]; animals[0] = new cat(); animals[1] = new dog(); for (int i = 0; i < animals. length; i++) animals[i]. speak(); Cat and dog public class cat extends animal { void speak() { System. out. printf(meown); public class dog extends animal { void speak() { Dynamic binding: methods are selected at runtime based on the class of the object referenced, not the class of the variable that holds the object reference, example.