{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part70

Intro to Java Web-Notes_Part70 -...

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

View Full Document Right Arrow Icon
Image of page 1

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

View Full Document Right Arrow Icon
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern