The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Arrays of diferent Objects and call to their methods

Posted by Villanueva on April 24, 2001 at 12:43 PM

Hallo. Can somebody give me an answer please:

I want to have an array of objects of two diferent classes: e.g. "humanPlayer" and "computerPlayer". I want to scan the array "players" and run the method "calculateAnswer()" for a computerPlayer and "getAnswer()" for a humanPlayer.

How can I do it?

If I make:
class computerPlayer extends humanPlayer
and
Player[] players = new humanPlayer[10]
it works, but I have to define calculateAnswer() in the class humanPlayer, and it has nonsense.

How can I do that it works:
for(int = 0; i < 10; i++){
if(players[i].getClass().getName() == "ComputerPlayer")
players[i].calculateAnswer() ;
else players[i].getAnswer() ;
}
without having to write a method "calculateAnswer()" in class "humanplayer" ?

Thanks a lot.

Villanueva





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us