Intro to Java Web-Notes_Part70

Intro to Java Web-Notes_Part70 -...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: System.out.println("AAAAAaaaaaaaa"); } class SubB extends Simple { public void run() { System.out.println("B!"); } } Since the call to Math. random( ) randomly generates a number between 0.0 and 1.0, about half the time a SubA object is created and half the time a SubB object is created. Therefore, at compile time, there is no way to know which run method is to be called by this line of code: ref.run(); It is only possible to determine which run method will be executed at the time the line of code is performed, by following the reference to the object and discover if it is a SubA object to execute a SubA run method, or if it is a SubB object to execute a SubBrun method. It is impossible to determine the correct run method at compile time. Determining the correct method at run-time is called late binding and this the trait known as polymorphism. fiuflfl will rum} prinilz'zi‘AAH ’ kfiaaafaifi ref amid 58‘2"}; S If you run the latest version of the SimpleApp application many times, about half the time the output will be AAAAAaaaaaaaa and about half the time it will be B! Exercises 1. Modify the Farm class’s main method so that it creates an array of ten farm animals (Cats and Cows). One array can be used to keep track of both types of animals (the array should be of type Animal). Set the array up so that it contains a random mixture of Cows and Cats. Then use a for loop to have each animal report its sound. The array has the name arr, then the loop should look something like this: for (int i = O; i < arr.length; i++) arr[i].sound(); Hnma - About Us Copyright © 2006 by Kiowok, Ann Arbor, Michigan, USA ...
View Full Document

Page1 / 2

Intro to Java Web-Notes_Part70 -...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online