CS 18000 Lecture 14: Polymorphism

58 views4 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents